- PHP code
$string = <<<str …………………… <img src="/img/pvistely/1.jpg" /> <img src="/img/pvistely/2.jpg" /> <img src="/img/pvistely/3.jpg" /> ……………………str; $pattern = array('<img src="/img/pvistely/1.jpg" />','<img src="/img/pvistely/2.jpg" />','<img src="/img/pvistely/3.jpg" />'); $replace = array('[img]1.jpg[/img]','[img]2.jpg[/img]','[img]3.jpg[/img]'); echo preg_replace($pattern, $replace, $string);
得到这样的结果:
- PHP code
…………………… <[img]1.jpg[/img]> <[img]2.jpg[/img]> <[img]3.jpg[/img]> ……………………
为什么会多出两个尖括号 < > ?
要怎么才能把这个< > 也替换掉?
------解决方案--------------------
$pattern = array('<<img src="/img/pvistely/1.jpg" />>','<<img src="/img/pvistely/2.jpg" />>','<<img src="/img/pvistely/3.jpg" />>');
------解决方案--------------------
你用的是 preg_replace
$pattern 项中两端的<>被当做规则串的分界符了
用 str_replace 就可以了