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)

function internal_links($str, $links, $limit=-1) {
foreach($links AS $link){
$pattern[$k] = "~\b({$link['phrase']})\b~i";
$replace[$k] = '<a href="'.$link['link'].'">\\1</a>';
return preg_replace($pattern,$replace,$str, $limit);

echo internal_links("süße knuffige Beagle Welpen ab sofort",
'link'=>"";)), -1);

süße knuffige<a href="";>Beagle</a> <a href="";>Welpen</a> ab



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",

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

Thank you for any help,


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'=>"";), array('phrase'=>"Welpen", 'link'=>"";));

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,


PHP General Mailing List (
To unsubscribe, visit:

Reply via email to