Jump to content

[SOLVED] preg_replace /images


SchweppesAle

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/159742-solved-preg_replace-images/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.