I need to change the src path (not necessarily http for all img in a blog post body. I prefer to use preg match / replace if possible For example: <img src="b.image.com/kittykat.png" /> would become <img src="/images/kittykat.png" /> Code (markup):
$string = ' <img src="b.image.com/kittykat.png" />'; preg_match_all('/(src)=("[^"]*")/i',$string,$matches); $src = explode("/",$matches[2][0]); echo '<img src="/images/'.$src[1].'" />'; PHP:
Or you could just get it in one go: $string = ' <img src="b.image.com/src/kittykat.png" />'; preg_match('/src\=[\"\\\']{0,1}.*?\/([a-zA-Z0-9\_\-\+\s]+\.[a-zA-Z]+)[\"\\\']/i',$string,$matches); //$matches[1] contains the filename PHP:
This definitely works but I can't see how to use it to modify my database fields (the posts). I think I need to match everything before the filename not the filename so I can you preg_replace right?