Re: stripping enclosed text from PHP code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



http://www.phpcompiler.org/

Satyam

----- Original Message ----- From: "Winfried Meining" <wim@xxxxxxxxx>
To: <php-general@xxxxxxxxxxxxx>
Sent: Sunday, April 09, 2006 10:20 PM
Subject:  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




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux