Regular Expression, Parsing Img Src Content And Replacing It With Another Links
I need to make next feature on my site: user writing an article and attach image in it, images are often stored not on localhost. I need to download this images to localhost and re
Solution 1:
A Php DOMDocument Example how to manipulate HTML image tags.
$dom=new DOMDocument();
$dom->loadHTML($html_src);
$imgs = $dom->getElementsByTagName('img');
foreach($imgsas$img){
$img_src = $img->getAttribute('src'); // will give you the src String//do something here$img->setAttribute('src',$new_src); // change your src= value
}
You can manipulate everything with setAttribute
when the Attribute exist.
If you want be sure, that src
is set, then you can use hasAttribute
Solution 2:
I think you need preg_replace_callback but keep in mind that downloadImage
may fail. so handle the failure gracefully (a fallback image or a retry queue)
Post a Comment for "Regular Expression, Parsing Img Src Content And Replacing It With Another Links"