可能重复: Regex to change format of all img src attributes
你好,
我想替换内容数据库字段中的图像路径。
我有以下内容
preg_replace("/src='(?:[^'\/]*\/)*([^']+)'/g","src='newPath/$2'",$content);
哪个工作正常
src="/path/path/image.jpg"
但失败了
src="http://www.mydomain.com/path/path/image.jpg"
有什么帮助可以绕过这个问题吗?
不要为此使用正则表达式。使用类似HTML语法分析器 Simple HTML DOM .
$html = file_get_html('http://www.example.com/sourcepage.html'); foreach($html->find('img') as $element) { $new_src = "Do stuff with new src here"; $element->src = $new_src; } echo $html; // Output new code