Jump to content

For Each Loop?? Array!!


gaza165

Recommended Posts

$pattern = "/http:\/\/[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+/i";
$match = preg_match_all($pattern, $body, $matches,1);
$gaz = $body;
$i = 0;
                        
foreach($matches[0] as $url){   
$replacement = "%TOKEN".$i."%";   
$gaz = preg_replace("@$url@", $replacement, $gaz, 1);
$i++;
}
               
$phrase = getSentences($gaz);
               
foreach($matches as $value) {
               
print_r($value);
            
}

 

I need a foreach loop to loop through the matches that are found and replace the %TOKEN0%, %TOKEN1% etc with the image urls.

 

Someone must understand this code!!

 

HELP!!

Link to comment
https://forums.phpfreaks.com/topic/119624-for-each-loop-array/
Share on other sites

I have updated my ForEach loop and this is where i am at.

 

$phrase = getSentences($gaz);

foreach($matches as $value) {

$string = getSentences($gaz);

$i=0;
$new = str_replace("%TOKEN".$i."%", $value[$i], $string);
$i++;
}
echo $new;

 

this seems to only replace the first token, i increment the $i variable by 1 so surely when the loop repeats it will replace the TOKEN1??

Link to comment
https://forums.phpfreaks.com/topic/119624-for-each-loop-array/#findComment-616323
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.