views:

36

answers:

1

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

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?

+2  A: 

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
Pekka
Suggested third party alternatives that actually use DOM instead of String Parsing: [phpQuery](http://code.google.com/p/phpquery/), [Zend_Dom](http://framework.zend.com/manual/en/zend.dom.html) and [FluentDom](http://www.fluentdom.org).
Gordon
Thank job is done very easily!!!!
ntan