Page 1 of 1

Highlighting PHP Syntax

Posted: Mon Sep 29, 2003 10:55 am
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

Posted: Mon Sep 29, 2003 11:30 am
by toms100
im not too sure what tags you mean, try to put them in code tags:)
also have look at Preg

Posted: Mon Sep 29, 2003 5:40 pm
by junky
just do a regex on "<?php" and and a '[' char before and a ']' after.
do the same thing for the string "?>".

Posted: Mon Sep 29, 2003 5:52 pm
by Nay
regex? a what? I still have no clue how to get the string between the tags.

-Nay

Posted: Mon Sep 29, 2003 5:58 pm
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.

Posted: Mon Sep 29, 2003 5:59 pm
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

Posted: Mon Sep 29, 2003 6:01 pm
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

Posted: Mon Sep 29, 2003 6:07 pm
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.

Posted: Mon Sep 29, 2003 6:52 pm
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!

Posted: Mon Sep 29, 2003 7:43 pm
by McGruff
You could try htmlspecialchars(). This changes < and > to < and >

Or, maybe just view source in your browser?

Posted: Mon Sep 29, 2003 7:47 pm
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?

Posted: Mon Sep 29, 2003 7:56 pm
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

Posted: Tue Sep 30, 2003 9:49 am
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

Posted: Fri Oct 03, 2003 8:23 pm
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

Posted: Sat Oct 04, 2003 11:26 am
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;
?>