Re: preg_replace question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux