I think there is an easier solution.

Check if the post contains <?php and ?>, if it doesn't, ask the user to
revise the posting.

If they submit it again without php tags we assume it's a plain text
posting.

Would this not work?

Also, I can't post to notes with the nntp server - perhaps we should talk
about this on phpdoc?

Kind Regards,
Aidan


"Jakub Vrana" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
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 revise
it.

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

Jakub Vrana

Reply via email to