The Little Guy Posted May 7, 2012 Share Posted May 7, 2012 Say I have a chunk of code that looks like this (contains imagesx): <span style="color: #0000BB">$width </span><span style="color: #007700">= </span><span style="color: #0000BB">imagesx</span><span style="color: #007700">(</span><span style="color: #0000BB">$img</span><span style="color: #007700">); <br /> within the code I would like to make all the php functions link to php.net I thought that the following code would work, but it does not, well because there is no spaces around the above word public function phpLinks($string){ $words = explode(" ", $string); $str = array(); foreach($words as $word){ if(function_exists($word)){ $str[] = preg_replace("/$word/i", '<a href="//php.net/'.strtolower($word).'">'.$word.'</a>', $word); }else{ $str[] = $word; } } return implode(" ", $str); } I now can not think of a good way to do this, so I am asking does anyone have any suggestions? Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 7, 2012 Share Posted May 7, 2012 You will probably have to have a set of delimiters you look for - spaces would be one, space and . would be another, and in your example > < would be a pair. It might be near impossible to get all the cases, but most of them should be easy enough. Quote Link to comment Share on other sites More sharing options...
kicken Posted May 7, 2012 Share Posted May 7, 2012 Your best bet would be to obtain a list of function names from php.net probably and then do a replace for each one, of the form: preg_replace("/\b($word)\b/", '<a href="http://www.php.net/$1">$1</a>', $str); The \b's should let you catch the word in most situations properly, and the list of possible names will help make sure your links are accurate. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 7, 2012 Share Posted May 7, 2012 Your best bet would be to obtain a list of function names from php.net probably and then do a replace for each one, of the form: preg_replace("/\b($word)\b/", '<a href="http://www.php.net/$1">$1</a>', $str); The \b's should let you catch the word in most situations properly, and the list of possible names will help make sure your links are accurate. How would you make sure that strtotime links to strtotime and not just time, or even worse, a kludge of links that all have time in the name? Quote Link to comment Share on other sites More sharing options...
kicken Posted May 7, 2012 Share Posted May 7, 2012 How would you make sure that strtotime links to strtotime and not just time, or even worse, a kludge of links that all have time in the name? That's what the \b's are for. /\b(time)\b/ does not match 'strtotime' but it will match 'time'. The only issues with a generic replace like this are: 1) It will pick up variables named like functions, eg $time=13992; 2) It will pick up function names inside strings, eg: echo "The current time is:"; You'd need a little more processing to handle things like that. A more advanced regex might do it but my regex-fu is not that strong. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 7, 2012 Share Posted May 7, 2012 Ah I had to look up \b, that's really useful. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.