Jump to content

Turn php function text into links


Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.