Miteshsach86 Posted October 25, 2010 Share Posted October 25, 2010 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: 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 Link to comment https://forums.phpfreaks.com/topic/216794-parallel-foreach-loops/ Share on other sites More sharing options...
The Little Guy Posted October 25, 2010 Share Posted October 25, 2010 I believe something like this should work (untested): $opt = 'Some String to search'; for($i=0;$i<count($wds);$i++){ $quote = preg_quote($words_array[$i]); $opt = preg_replace("~ $quote ~", $urls_array[$i], $opt); // added spaces to match ONLY words, and not urls } echo $opt; Link to comment https://forums.phpfreaks.com/topic/216794-parallel-foreach-loops/#findComment-1126291 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.