php
  • preg-replace
  • 2011-10-24 34 views 0 likes 
    0

    我有问题,这个“简单”的代码:PHP preg_replace函数与包含字符串|,[,]签署

    $img_oryg = '<igm class="aligncenter" src="(oryginal img source)" alt="some text | some text">'; 
    $replace_with = '<div class="ivcs_holder"><igm class="aligncenter" src="(oryginal img source)" alt="some text | some text" /></div>'; 
    $content = preg_replace("#$img_oryg#s", $replace_with , $contetn); 
    echo $content; 
    

    这是ofcourse一个示例代码,和一切是编码一个循环,它重叠图像内与divs,还doeas一些其他的东西。 问题出在|可以添加到图像的alt标签。如果它存在,那么代码表现奇怪,结果不正确。

    有人不知道如何处理这样的字符串?

    +1

    对于umptillionth + 1时间:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454不要使用正则表达式来解析HTML 。 –

    +0

    好吧,现在我知道,我不应该使用正则表达式来解析HTML - 那么我应该使用什么来获得相同的结果?[? –

    +0

    @MarcB奇怪的是,一些XML/HTML解析器使用正则表达式 –

    回答

    0

    ,你在你的正则表达式模式运行preg_quote应该是不错的方法长。 |字符在正则表达式中有特殊的含义,所以这就是为什么它会导致问题。

    +0

    或者使用str_replace函数中有人评论:) 哦提到,和Marc B的答复反弹很有帮助,现在职高我会记得直到死亡有同样的结果写的;) –

    相关问题