开发者

php regexp for get images [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Regex to change format of all img src attributes

Hi,

I want to replace the image path in my content db field.

I have the following

开发者_JS百科preg_replace("/src='(?:[^'\/]*\/)*([^']+)'/g","src='newPath/$2'",$content);

which is working fine for

src="/path/path/image.jpg"

BUT fails ON

src="http://www.mydomain.com/path/path/image.jpg"

Any help to bypass this problem?


Don't use regular expressions for this. Use a HTML parser like 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜