tags:

views:

39

answers:

2

Hello again, I have the following code with me, it produce the img title of specified text,

let say the title = waterfallfountainpicture20x20, how do i replace the word fountain with lagoon so it would become waterfalllagoonpicture20x20?

preg_match_all('/<img[^>]+>/i',$html, $result);
$img = array();
foreach( $result[0] as $img_tag)
{
    preg_match_all('/(title)="([^"]*)"/i',$img_tag, $img[$img_tag]);
}

//print_r($img);
foreach ($img as $imgg)
 echo $imgg[2][0];

Thank you very much for your help

A: 

Not sure I understand your question correctly. You could try using preg_replace instead of preg_match_all.

maschka
+1  A: 

well since you've already pulled out the image tags, and it sounds like you know exactly what you are replacing (static text), might be better for you to just use str_replace but if you want to go the regex route...

preg_match_all('/<img[^>]+>/i',$html, $result);
$img = array();
$sub = "fountain";
$rep = "lagoon";
foreach( $result[0] as $img_tag)
{
    $img[] = preg_replace('/(title\s?=\s?["\'][^"\']*)'.$sub.'/i','${1}'.$rep,$img_tag);
}
Crayon Violent
this does replace the string however text become <img title="waterfalllagoonpicture20x20" />, instead of waterfalllagoonpicture20x20, how do i use str_replace? i am new to this.
BobDylan