SchweppesAle Posted May 26, 2009 Share Posted May 26, 2009 hi, I'm trying to correct the following code so that it replaces image strings which match the following /images/ or images/ with $domain.'/images'. I tried two different methods: $introtext = $row['introtext']; $pattern = '/images/'; $replacement = $domain.'/images'; $introtext = preg_replace($pattern, $replacement, $introtext); this works, however it will also apply to anything which has the word "image" in its name. I also tried the following. if(preg_match_all('#src=[\'"][^\'"]+[\'"]#i', $introtext, $matches)) { foreach($matches[0] as $val) { $introtext = str_replace($val, str_replace('/images/',$domain.'/images',$val), $introtext); } } Doing something like this would probably be more appropriate. It didn't seem to have any effect on the string though. image urls use the following format src="images/stories/storyimages/softwarereview-150.jpg". The first method will actually replace the image in storyimage which confuses me since I included \ on both sides of the word image in $pattern. I tried using the pattern 'src="images/' but didn't have much luck. Quote Link to comment https://forums.phpfreaks.com/topic/159742-solved-preg_replace-images/ Share on other sites More sharing options...
Ken2k7 Posted May 26, 2009 Share Posted May 26, 2009 I would just do it literally - <?php $introtext = str_replace('src="images/','src="'.$domain.'/images', $introtext); Quote Link to comment https://forums.phpfreaks.com/topic/159742-solved-preg_replace-images/#findComment-842593 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.