For Programmers: Free Programming Magazines  


Home > Archive > PHP Mirrors > July 2004 > PHP tags in notes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author PHP tags in notes
Jakub Vrana

2004-07-25, 3:56 pm

PHP code is syntax highlighted inside user notes. It's good. But users
often don't use PHP tags to delimit the code so it can't highlighted.
It's bad.

I made a patch to add-note.php to check if the note contains the PHP
tags (easy part) and if not than if it should contain it (hard part).

I was thinking about approaches to the problem "Is the part of the
text PHP code or plain text?" and I made up this:

1. PHP code is usually more various than plain text. E.g. something
like simple "strlen(highlight_string($code, true)) >= 4*strlen($code)"
can work.
(+) Simple
(-) Parts with mixed plain text and PHP code can be marked as code

2. Remove PHP strings, comments, variables, reserved words, functions,
etc. by regular expressions and check if only symbols remain.
(+) Works quite well with not very difficult code
(-) Regular expressions sometimes remove something what should remain

3. Tokenize the text and check if it contains only PHP functions in
the place of words.
(+) Works quite well even with difficult code
(+-) Some syntax errors causes the text is recognized as plain text
(-) Code like "if (true) foo();" causes the text is recognized as plain text

4. Check the syntax of a text.
(+) Simple, clear and powerful
(+-) All syntax errors causes the text is recognized as plain text
(-) Syntax checking function is only in PHP 5

5. Check if the text contains enough PHP symbols not used in plain
text often ($;{}=() ...).
(+) With good weights of symbols can work well, weights can be determined automatically
(-) The same as 1

I tried 1, 2 and 3. 3 works best from them so I am sending a patch
using it. How it works:

Check if the note already contains <? symbol.
If not, separate the note by \n\n and check if any part is PHP code.
If yes, add PHP tags around appropriate parts and tell the user to reviseit.

If you agree, I will run my script on current user notes and add PHP
tags where appropriate (with human supervision).

Jakub Vrana
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com