Parallel Foreach loops

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Miteshsach86
Forum Newbie
Posts: 7
Joined: Thu Oct 07, 2010 4:41 am

Parallel Foreach loops

Post by Miteshsach86 »

Hi Guys,

I'm really :? [confused] about this however what I have so far is create a simple HTML form, instructing the end user to enter a list of keywords with links to replace them with in the following format: A keywords followed by a comma and then URL and semicolon:

For example:
birds,http://www.birdwatching.com/;
snakes,http://www.whales.com/;
horses,http://www.horse.com/;

What I have done next is split each word with its associated url after each newline and then placed all the words into 1 array and all the urls into another array, using the following code:

Code: Select all

preg_match_all('/(.*),(.*);/',$wordsurls, $phrase);
$wds = $phrase[1];
$rurls = $phrase[2];
foreach($wds as $wd) {
	$words_array[] = $wd;
}
foreach($rurls as $rurl) {
	$urls_array[] = $rurl;
}
All that works perfectly fine but now I wish to search a string to find each of those keywords, and if they exist I would like to replace them with the URL. I believe this can be done using Parallel Array matching but do not know how to go accomplishing this. Could someone please help me?

Thank you in advance.

M
Last edited by Weirdan on Mon Oct 25, 2010 1:07 pm, edited 1 time in total.
Reason: added syntax highlighting
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Parallel Foreach loops

Post by twinedev »

from: http://php.net/str_replace

Code: Select all

// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);
so with what you have, you should be able to do something like:

Code: Select all

$changed_text = str_replace($words_array,$urls_array,$orignal_text);
Post Reply