Home > Backend Development > PHP Tutorial > preg_replace 替换的有关问题

preg_replace 替换的有关问题

WBOY
Release: 2016-06-13 13:13:38
Original
895 people have browsed it

preg_replace 替换的问题

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
    $string = 
        <img  src="/img/pvistely/2.jpg" alt=" preg_replace 替换的有关问题 " >
        <img  src="/img/pvistely/3.jpg" alt=" preg_replace 替换的有关问题 " >
        ……………………
str;
    $pattern = array('<img  src="/img/pvistely/1.jpg" alt=" preg_replace 替换的有关问题 " >','<img  src="/img/pvistely/2.jpg" alt=" preg_replace 替换的有关问题 " >','<img  src="/img/pvistely/3.jpg" alt=" preg_replace 替换的有关问题 " >');
    $replace = array('[img]1.jpg[/img]','[img]2.jpg[/img]','[img]3.jpg[/img]');
    echo preg_replace($pattern, $replace, $string);

Copy after login


得到这样的结果:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
    …………………… 
     
     
     
    ……………………


Copy after login



为什么会多出两个尖括号 ?
要怎么才能把这个 也替换掉?

------解决方案--------------------
$pattern = array('>','>','>');
------解决方案--------------------
你用的是 preg_replace
$pattern 项中两端的被当做规则串的分界符了

用 str_replace 就可以了
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template