PDA

View Full Version : Highlight and link function names inside code


Admin
02-26-2002, 10:00 PM
Here's a nice idea I got from another site, which highlights all function names in PHP code and links to their documentary. In this hack the functions are linked to PHP.NET's docs.
Needless to add this is perfect for sites that deal with PHP coding. :)

Demo:
if(!$dohtml) { // kill any rogue html code
// $bbcode=str_replace("&","&",$bbcode);
$bbcode=str_replace("<","<",$bbcode);
$bbcode=str_replace(">",">",$bbcode);
$bbcode=str_replace("<","&lt;",$bbcode);
$bbcode=str_replace(">","&gt;",$bbcode);
}
$bbcode=nl2br($bbcode);
(also works inside [php] tags)

To install this hack download the attached file (linkfuncs.hack.php), upload to your /admin folder and use vbHacker (https://vborg.vbsupport.ru/showthread.php?s=&threadid=36378).

Have fun! :D

Lesane
02-27-2002, 02:56 PM
Thats great, i saw it on evilwalrusdotcom. Great addition, thanks :up:

Admin
02-27-2002, 03:39 PM
Yup, that's where I saw it too! Although they are using their own docs for the functions.

JJR512
02-28-2002, 06:29 AM
Well I see you're using it here. I have a complaint about this hack being used here.

Sometimes people post code here that is part of a hack. I like to keep local text files of hack instructions so that I can have a record of everything I've done, and can quickly re-apply each hack after a vB upgrade without having to search all over the forums for every hack I want. So when someone posts code directly in a message, I have to copy that and put it in a text file myself.

The problem is that some editors, if you copy a hyperlink into it, will spell out the full URL of the link. For example, WordPad. I normally use Notepad on my Win2k computer, but for reasons which don't matter here, lately I've been having to access the Internet from my parents' computer, which only has WinMe on it, and the Notepad on WinMe sucks (it doesn't recognize any of the standard keyboard shortcuts and the search feature is limited, too). So I'm kind of forced to use WordPad here as my text editor, and even if you start a new document in text mode, if you paste anything with formatting into the window it still keeps the formatting, it just takes the formatting away when you save it as a text file.

The problem is that if I try to copy, for example, this:
$post[joindate]=vbdate($registereddateformat,$post[joindate]);
...WordPad turns it into this:
$post[joindate]=vbdate <http://www.php.net/date>($registereddateformat,$post[joindate]);
If it's just one line, I might catch it, but if it's a big block of code, I might not. I shouldn't have to worry about it.

Aside from all that, your hack didn't even work right for the example I just posted. I guess it just saw date( and assumed it was a PHP function? But it's not. It's vbdate(, a custom vB-only function.

Perhaps there should be a way for a user (like me) to turn this feature off. I don't need it and won't use it, and it's just going to get in my way. I don't really see it being used very much in general, anyway; at least for me, when I'm getting code out of these forums is not the time I want to know what a specific function does. It's when I'm editing code later on that I might want to know what something does.

Just my 2?. :)

Xelation
03-01-2002, 01:15 AM
I just installed it with no problems, great hack firefly!

TECK
03-01-2002, 02:07 AM
Originally posted by JJR512
The problem is that some editors, if you copy a hyperlink into it, will spell out the full URL of the link.convince your parents to let you install EditPlus2 or...... UltraEdit90 (my preferate.. ;))

Admin
03-01-2002, 11:26 AM
a) The bug where vbdate() is parsed as date() and unlink() is parsed as link() should be fixed now.
unlink($bob);
$foo = vbdate($bar);

b) I will try to add a user option to enable/disable this hack soon.

Admin
03-01-2002, 11:48 AM
Done. Feel free to turn this off in your options.

Admin
03-26-2002, 07:32 AM
Uploaded a vbHacker version. :)

Visionray
03-29-2002, 05:35 AM
nice hack!

Slynderdale
06-04-2002, 06:40 AM
i have it where members can enable and disable it in options but for some reason, even if you disable it, it still shows, heres the code


if ($bbuserinfo[userid]==0 or $bbuserinfo[showfunclinks]==1) {
/* Highlight function names in [php] */
$allfuncs = get_defined_functions();
$searches = array();
$replaces = array();
foreach ($allfuncs['internal'] as $name) {
$searches[] = "/([^a-z])$name(<\/font><font color=\"" . ini_get('highlight.keyword') . "\">){0,1}( {0,1})\(<\/font>/i";
$replaces[] = "\\1<a href=\"http://www.php.net/$name\" target=\"_blank\">$name</a>\\2\\3(</font>";
}
$buffer = preg_replace($searches, $replaces, $buffer);
/* end Highlight function names in [php] */
}


it still highlights and links the code and php boxes

Admin
06-04-2002, 06:46 AM
Make sure $bbuserinfo is global in that function.

Slynderdale
06-04-2002, 06:55 AM
Hmm, it got rid of the links but the highlighting is still there and it shows ?> at the end of the php code

Slynderdale
06-04-2002, 07:05 AM
also the ?> if its enabled and some links arent linked as they do in the code bbcode

Slynderdale
06-04-2002, 07:06 AM
heres my php function

// ###################### Start phphighlite #######################
function phphighlite($code) {
//PHP 4 only

global $bbuserinfo;

if (floor(phpversion())<4) {
$buffer=$code;
} else {
$code = str_replace("<br>", "", $code);
$code = str_replace("<br />", "", $code);
$code = str_replace("&gt;", ">", $code);
$code = str_replace("&lt;", "<", $code);

$code = str_replace("&amp;", "&", $code);
$code = str_replace('$', '\$', $code);
$code = str_replace('\n', '\\\\n', $code);
$code = str_replace('\r', '\\\\r', $code);
$code = str_replace('\t', '\\\\t', $code);

$code = stripslashes($code);

if (!strpos($code,"<?") and substr($code,0,2)!="<?") {
$code="<?\n".trim($code)."\n?>";
$addedtags=1;
}
ob_start();
$oldlevel=error_reporting(0);
highlight_string($code);
error_reporting($oldlevel);
$buffer = ob_get_contents();
ob_end_clean();
if ($addedtags) {
$openingpos = strpos($buffer,'&lt;?');
$closingpos = strrpos($buffer, '?');
$buffer=substr($buffer, 0, $openingpos).substr($buffer, $openingpos+5, $closingpos-($openingpos+5)).substr($buffer, $closingpos+5);
}
$buffer = str_replace("&quot;", "\"", $buffer);
}

if ($bbuserinfo[userid]==0 or $bbuserinfo[showfunclinks]) {
/* Highlight function names in [php] */
$allfuncs = get_defined_functions();
$searches = array();
$replaces = array();
foreach ($allfuncs['internal'] as $name) {
$searches[] = "/([^a-z])$name(<\/font><font color=\"" . ini_get('highlight.keyword') . "\">){0,1}( {0,1})\(<\/font>/i";
$replaces[] = "\\1<a href=\"http://www.php.net/$name\" target=\"_blank\">$name</a>\\2\\3(</font>";
}
$buffer = preg_replace($searches, $replaces, $buffer);
/* end Highlight function names in [php] */
}

return "</normalfont><table border=\"0\" align=\"center\" width=\"90%\" cellpadding=\"3\" cellspacing=\"1\"><tr><td><font face=\"Arial\" size=1 color=#616889><smallfont><b>PHP:</b></smallfont> <smallfont>(double click the code to copy it)</smallfont></font></td></tr><tr><td class=\"phptable\"><pre>" . $buffer . "</pre></td></tr></table><normalfont>";
}

Slynderdale
06-05-2002, 04:33 AM
any one know whats wrong?

Slynderdale
06-06-2002, 01:34 PM
^bump^

Boofo
06-23-2002, 05:40 PM
This is strange. For some reason I don't see this in my options panel.

Show links to function names in code?

I installed the exact same version you have here. :confused:

Originally posted by FireFly
Done. Feel free to turn this off in your options.

php-resource.de
08-21-2002, 05:02 PM
Originally posted by Slynderdale
any one know whats wrong?

did you get it to work?