On 25 January 2011 12:04, Merlin Morgenstern <merlin_x@xxxxxxxxxxx> wrote: > Am 25.01.2011 12:31, schrieb Merlin Morgenstern: >> >> Am 24.01.2011 18:08, schrieb Alex Nikitin: >>> >>> If you declare your arrays, and set k to 0 first, put quotes around array >>> values and use the correct limit (you can default to -1), you will get >>> results, here is code and example (hopefully this helps you) >>> >>> >>> <?php >>> function internal_links($str, $links, $limit=-1) { >>> $pattern=array(); >>> $replace=array(); >>> $k=0; >>> foreach($links AS $link){ >>> $pattern[$k] = "~\b({$link['phrase']})\b~i"; >>> $replace[$k] = '<a href="'.$link['link'].'">\\1</a>'; >>> $k++; >>> } >>> return preg_replace($pattern,$replace,$str, $limit); >>> } >>> >>> echo internal_links("sÃÃe knuffige Beagle Welpen ab sofort", >>> array(array('phrase'=>"beagle", >>> 'link'=>"http://google.com"),array('phrase'=>"welpen", >>> 'link'=>"http://wolframalpha.com")), -1); >>> >>> Output: >>> sÃÃe knuffige<a href="http://google.com">Beagle</a> <a href=" >>> http://wolframalpha.com">Welpen</a> ab >>> >>> ~Alex >>> >> >> Hello, >> >> thank you all for your help. It seems that I am building the array >> wrong. Your code works with that array: >> >> $internal_links = array(array('phrase'=>"beagle", >> 'link'=>"http://google.com"),array('phrase'=>"welpen", >> 'link'=>"http://wolframalpha.com")); >> >> I am pulling the data out of a DB and am using this code: >> while ($row = mysql_fetch_object($result)){ >> $internal_links[$row->ID]['phrase'] = $row->phrase; >> $internal_links[$row->ID]['link'] = $row->link; >> } >> >> You build the array different, could you help me to adapt this on my >> code? I tried $internal_links['phrase'][] as well, but that did not help >> either. >> >> Thank you for any help, >> >> Merlin > > > HI Again :-) > > the building of my array seems fine. Here is what goes wrong: > > If you use this array: Â$internal_links = array(array('phrase'=>"Beagle > Welpen", 'link'=>"http://wolframalpha.com"), array('phrase'=>"Welpen", > 'link'=>"http://google.com")); > > Then it will fail as well. This is because the function will replace "Beagle > Welpen" with the hyperlink and after that replace the word "welpen" inside > the hyperlink again with a hyperlink. > > Is there a function which will not start looking for the words at the > beginnning of the text for each replacement, but simply continue where it > did the last replacement? > > Thank you for any help, > > Merlin > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > The solution I've used in the past for this sort of issue (recursive replacements when not wanted) is to replace each known part with a unique placeholder. Once the initial data has been analysed and the placeholders are in place, then replace the placeholders with the correct value. So, rather than ... $internal_links = array ( array('phrase'=>"Beagle Welpen", 'link'=>"http://wolframalpha.com"), array('phrase'=>"Welpen", 'link'=>"http://google.com") ); Use ... $internal_links = array ( array('phrase'=>'Beagle Welpen', 'link'=>'_RAQ_TAG_1_'), array('phrase'=>'Welpen', 'link'=>'_RAQ_TAG_2_'), array('phrase'=>'_RAQ_TAG_1_' 'link'=>'http://wolframalpha.com'), array('phrase'=>'_RAQ_TAG_2_' 'link'=>'http://google.com'), ); By keeping them in the above order, each phrase will be replaced in the same way. Obviously, if your text includes _RAQ_TAG_1_ or _RAQ_TAG_2_ then you will have to use more appropriate tags. Richard. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php