php3-list | 2000051
Date: 05/01/00
- Next message: admin <email protected>: "[PHP3] Unsubscribe Confirmation"
- Previous message: Mike Dennis: "[PHP3] Xitami & PHP & Win32 & FileUpload"
- In reply to: Vincent Driessen: "[PHP3] Syntax Highlight"
- Next in thread: Patrick Meisel: "Re: [PHP3] Syntax Highlight"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
* Vincent Driessen <vinnie.driessen <email protected>> [000501 00:41] wrote:
> Hi there,
>
> I'm doin' an algorithm to highlight Pascal code. When a user types is him
> code in a form on my site and clicks the format button, a PHP script is
> executed to start syntax highlighting.
>
> So far, It works like this. I made a function SplitCode. It split the code
> into 3 pieces:
> - First piece (no strings or comments)
> - A string or a comment
> - The rest, can be anything. This part is recursively parsed again to
> the SplitCode function
>
> All parts work smooth by now (partly thanks to some of you guys!). I have an
> array with reserved words which I want to apply to the first part. Therefor,
> I thought just replacing all reserved words in the first part by the same
> string, surrounded by <b></b> tags. Not...!! :-(
>
> What happened was this (of course, you say afterwards):
>
> <b>uses</b>
> W<b>in</b><b>do</b>ws;
>
> While it had to be:
>
> <b>uses</b>
> Windows;
>
> You see... ("in" and "do" are reserved!)
>
> Has anybody got a better idea to do this? Or can anybody tell me how to
> parse all words in a string, then check if it is reserved and then replacing
> it or something?
I would use an ereg_replace() to do it, something like this:
$string = "this is a test";
$string = ereg_replace("([^A-Za-z0-9]|^)(((is)([^A-Za-z0-9]|$))+)", "\\1<b>\\2</b>", $string);
echo "<html><head></head><body>$string</body></html>";
Pick up a book on regex, it will explain that basically I'm looking
for "is" where the neighboring characters are not alphanumeric or
are the beginining or end of line, you may need to also include _
in the []'s basically whatever charaters can be part of a reserved
word or identifier.
This is actually more complicated than it seems, the problem I had
was that the regex I was initially going to give you wouldn't work
if you had: $string = "is is foo"; // second 'is' doesn't get
bolded
this regex works on that, in fact you can get more complicated by
combining the regex like so:
$string = ereg_replace("([^A-Za-z0-9]|^)(((is|do|while)([^A-Za-z0-9]|$))+)",
"\\1<b>\\2</b>", $string);
I don't know how much you'd gain/loose by doing that, but I'd check
it out if i had the chance and see if it helps when highlighting
large blocks of text with many reserved words.
Best of luck.
-- -Alfred Perlstein - [bright <email protected>|alfred <email protected>] "I have the heart of a child; I keep it in a jar on my desk."-- PHP 3 Mailing List <http://www.php.net/> To unsubscribe, send an empty message to php3-unsubscribe <email protected> To subscribe to the digest, e-mail: php3-digest-subscribe <email protected> To search the mailing list archive, go to: http://www.php.net/mailsearch.php3 To contact the list administrators, e-mail: php-list-admin <email protected>
- Next message: admin <email protected>: "[PHP3] Unsubscribe Confirmation"
- Previous message: Mike Dennis: "[PHP3] Xitami & PHP & Win32 & FileUpload"
- In reply to: Vincent Driessen: "[PHP3] Syntax Highlight"
- Next in thread: Patrick Meisel: "Re: [PHP3] Syntax Highlight"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

