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? Link to comment https://forums.phpfreaks.com/topic/262208-turn-php-function-text-into-links/ 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. Link to comment https://forums.phpfreaks.com/topic/262208-turn-php-function-text-into-links/#findComment-1343730 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. Link to comment https://forums.phpfreaks.com/topic/262208-turn-php-function-text-into-links/#findComment-1343787 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? Link to comment https://forums.phpfreaks.com/topic/262208-turn-php-function-text-into-links/#findComment-1343789 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. Link to comment https://forums.phpfreaks.com/topic/262208-turn-php-function-text-into-links/#findComment-1343808 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. Link to comment https://forums.phpfreaks.com/topic/262208-turn-php-function-text-into-links/#findComment-1343815 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.