Re: stripping enclosed text from PHP code

From: Date: Sun, 09 Apr 2006 20:58:31 +0000
Subject: Re: stripping enclosed text from PHP code
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-233638@lists.php.net to get a copy of this message
http://www.phpcompiler.org/ Satyam ----- Original Message ----- From: "Winfried Meining" <wim@csb.ki.se> To: <php-general@lists.php.net> Sent: Sunday, April 09, 2006 10:20 PM Subject: [PHP] stripping enclosed text from PHP code
Hi, I am writing on a script that parses a PHP script and finds all function calls to check, if these functions exist. To do this, I needed a function that would strip out all text, which is enclosed in apostrophes or quotation marks. This is somewhat tricky, as the script needs to be aware of what really is an enclosed text and what is PHP code. Apostrophes in quotation mark enclosed text should be ignored and quotation marks in apostrophe enclosed text should be ignored, as well. Similarly, escaped apostrophes in apostrophe enclosed text and escaped quotation marks in quotation mark enclosed text should be ignored. The following function uses preg_match to do this job. <? function stripstrings($text) { while (preg_match("/^(.*)(?<!\\\)('|\")(.*)$/", $text, $matches)) { $front = $matches[1]; $lim = $matches[2]; $tail = $matches[3]; while (preg_match("/^(.*)(?<!\\\)('|\")(.*)$/", $front, $matches)) { $front = $matches[1]; $tail = $matches[3] . $lim . $tail; $lim = $matches[2]; } if (!preg_match("/^(.*)(?<!\\\)$lim(.*)$/", $tail, $matches)) break; $string = $matches[1]; $tail = $matches[2]; while (preg_match("/^(.*)(?<!\\\)$lim(.*)$/", $string, $matches)) { $string = $matches[1]; $tail = $matches[2] . $lim . $tail; } $text = $front . $tail; } return($text); } ?> I noticed that this function is very slow, in particular because preg_match("/^(.*)some_string(.*)$/", $text, $matches); always seems to find the *last* occurrence of some_string and not the *first* (I would need the first). There is certainly a way to write another version where one looks at every single character when going through $text, but this would make the code much more complex. I wonder, if there is a faster *and* simple way to do the same thing. Is there btw a script freely available, which can parse PHP code and check for errors ? Any help is highly appreciated. Winfried -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#233638) next »