【发布时间】:2011-05-20 22:47:25
【问题描述】:
我有以下代码(php),它将匹配 img-src 并替换为新的 url
$rep = array('/', '+', '(', ')');
$with = array('\/', '\+', '\(', '\)');
$match_pattern = '/<img[^<]*src\s*=\s*\"'.str_replace($rep, $with, $source_url).'\"[^>]*>/iUu';
$img_replace_str = '<img src="'.$new_url.'" />';
$post_content = preg_replace($match_pattern, $img_replace_str, $post_content);
对于 src 为“http://www.example.com/a.jpg”的图像,没有问题,但对于 src 包含查询字符串如“http://www.example”的图像。 com/b.jpg?height=900",不匹配
我想匹配带有和不带有查询字符串的图像。
【问题讨论】:
-
不是答案,但您可能会发现 array_quote (php.net/manual/en/function.preg-quote.php) 会为您节省那些查找和替换..