Highlighting PHP Syntax

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Highlighting PHP Syntax

Post by Nay »

I recently came by a highlight_string() function from the documentation. I found it very useful to display my PHP scripts for my readers.

Anyhow, I wanted to take that function a little higher - but have no idea how right now.

I want to get the string between:

[<?php]

[/php?>]

I just added the <?php and php?> to avoid PHPBB from recognize them. Anyhow, how do you get the string between those two tags?

So then I can:

Code: Select all

$string = how_ever_you_do_it();
highlight_string($string);
Thanks,

-Nay
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

im not too sure what tags you mean, try to put them in code tags:)
also have look at Preg
junky
Forum Commoner
Posts: 30
Joined: Wed Sep 03, 2003 3:58 pm

Post by junky »

just do a regex on "<?php" and and a '[' char before and a ']' after.
do the same thing for the string "?>".
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

regex? a what? I still have no clue how to get the string between the tags.

-Nay
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Don't usually post scripts but here we go:

Code: Select all

<?php
// highlights strings between [ php] and [/php ] tags
// note pass $string by ref, so in use just:
//  phpHighlighter($string);
function phpHighlighter(&$string) 
{
    // initialise
    $replace = null;
    
    preg_match_all("#\[php\](.*?)\[/php\]#si", $string, $matches);

    $i = 0;
    foreach ($matches[1] as $value) 
    {
        $replace[$i] = '<div class="php">' . highlight_string('<?php ' . $value . '?>', 1) . '</div>';
        $i++;
    }    
    $string = str_replace($matches[0], $replace, $string);
}

?>
This is a handy tool: http://www.weitz.de/regex-coach/#install

PS: I just realised I've got a stylesheet class in there - adjust as required.
Last edited by McGruff on Wed Aug 10, 2005 5:06 pm, edited 3 times in total.
junky
Forum Commoner
Posts: 30
Joined: Wed Sep 03, 2003 3:58 pm

Post by junky »

hummm, a regex? how can i say more about regex?
a regex is related to pattern.
ya should do a search on google on perl regex.
ive followed like 20 hours in classes about regex, so its a bit hard to explain in really well here.
ya can search for regex tutorials, that would be nice.

if ya're still getting trouble, after searching and reading, of course, im gonna try to send ya tutorials that would help ya.

later
junky
Forum Commoner
Posts: 30
Joined: Wed Sep 03, 2003 3:58 pm

Post by junky »

McGruff: he seems to dont know whats a regex, so giving him the answer wont help him, i think.
he needs to read more and be able to make his owns regex, no? :D
later
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

junky wrote:McGruff: he seems to dont know whats a regex, so giving him the answer wont help him, i think.
he needs to read more and be able to make his owns regex, no? :D
later
Fair point - pushed for time but I maybe can explain more.

Regex stands for regular expressions. A very powerful tool for finding or replacing patterns in strings.

In php there's posix style ereg, eregi fns which should not be used since they're less efficient than the perl style peg_match, preg_replace etc.

Better still, use the string fns str_replace, strstr etc (if you can achieve what you want with these).

Syntax here: http://www.php.net/manual/en/pcre.pattern.syntax.php.
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Post by like_duh44 »

As McGruff posted, the php highlighter is great, but is there any way of highlighting the syntax of HTML? I would greatly appreciate it!
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

You could try htmlspecialchars(). This changes < and > to < and >

Or, maybe just view source in your browser?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Just adding some useful tips. Regarding [blocks] to [/block] replacements, consider downloading for example this very board (phpbb) and look at it's code in the files that handles posting. There you have good examples of img/code/php/bold/etc usage.

HTML syntax is interesting. Dont think there are, but it wouldn't be to hard to whip up. Anyone know the standards for coloring html?
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

i might say that can be done by knowing all the functions, put them in a array or string separated by commas and then when the str_replace finds it in the block of text it adds up the corresponding <span style="color: ?????">text</style> tags... and also for reserved words an array and all that stuff
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

It's either my brain packed with vast knowledge of unneccessary information and I can't stuff more in or I can't understand all the:

Code: Select all

#\[php\](.*?)\[/php\]#si
What is escaping what? For what? How? Man &gt;&lt;

And about coloring HTML, here:

http://chaosdreamers.com/dreamweaver.jpg

I took yahoo's html source code and put it into dreamweaver. They colored it. By studying it, you might be able to cook up a HTML colorer.

-Nay
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

hey nay, take a look at this:

Code: Select all

<?php
$text = "hola

Code: Select all

--BEGIN WITH 2 DASHES, TEXT HERE WOA...?>!@#$%^&*()END WITH TWO DASHES--
";
$array = array();
preg_match_all("/\[php\](.*)\[\/php\]/i",$text,$array);
print_r($array); //or echo $array[1];
?>

hope it helps u
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

this one will loop though all the [php tags and display em:

Code: Select all

&lt;?php
$text = &lt;&lt;&lt; END
&#1111;php]
&lt;?php echo "this"; ?&gt;
and more

Code: Select all

<?php
print_r($wut);
?>
mmmm
END;

preg_match_all("#\[php\](.*?)\[/php\]#si", $text, $array);
$i=0;
foreach($array as $key) {
if ($i>0) {
foreach ($key as $value) {
$result .= highlight_string($value,1)."\n";
}
}
$i++;
}
echo $result;
?>
Post Reply