preg_replace每个自定义字符,但URL

时间:2013-11-15 19:53:54

标签: php regex wordpress preg-replace

您好我正在使用WordPress,我想替换以下

$items = array( ".","?","!",",","؟","،",":","™","*" );

所以我编写了这个功能

function Huemix_content_filter_chars($content){
$items = array();
$items = array( ".","?","!",",","؟","،",":","™","*" );
$before='<span class="kindared">';
$after='</span>';
foreach ( $items as $k => $val ) {
    $content = preg_replace("|(\\$val)|","$before$1$after",$content);
     $content = preg_replace("|(<[^>]+?)($before(\\$val)$after)([^<]+?>)|","$1$3$4",$content);
}
return $content;
}
add_filter('the_content', 'Huemix_content_filter_chars');

我不明白什么是错的! 它工作,但不完全 它打破了这个链接

<a href="http://facebook.com/huemix.ly">Huemix Studio, Inc™</a>

它打印得像这样

<a href="http&lt;span class=" kindared"="">://facebook<span class="kindared">.</span>com/huemix<span class="kindared">.</span>ly"&gt;Huemix Studio<span class="kindared">,</span> Inc<span class="kindared">™</span></a>

但它没有打破第二个链接

    Follow Us On Facebook <a href="http://facebook.com/huemix.ly">Huemix Studio, Inc™</a>

Follow Us On Twitter <a href="http://twitter.com/HuemixStudio">Huemix Studio, Inc™</a>

你可以在网上看到我的问题

它正在制作我的帖子图片,这里是我的html帖子图片的输出

<div class="huemix-works "><img class='huemix-works-image' alt='Huemix Studio Works' src='http://alpha.huemix.ly/wp-content/uploads/Huemix_temp/NASER-GROUP1-698998_140x94.png' /><span class='huemix-works-overlay'></span><a class="huemix-works-zoom-icon huemix-fancybox" title="Huemix Studio Works" href="http://alpha.huemix.ly/wp-content/uploads/2013/11/NASER-GROUP1.png" rel="media">Zoom In</a></div>

请在线查看我的问题herehere,这是我的演示网站 请看它打破了一些事情,但另一件事情很好! 请帮助!

1 个答案:

答案 0 :(得分:0)

为什么不直接使用str_replace?

$items = array( ".","?","!",",","؟","،",":","™","*" );
$content = str_replace($items, '', $content);