Oct
12
PHP Regex - POSIX vs PCRE
October 12, 2007 |
PHP supports both POSIX Regex (POSIX Extended) and PCRE (Perl-Compatible) regular expression. For POSIX, the syntax is ereg, and for PCRE, the syntax is preg. According to PHP offical site documentation, PCRE is much (at least 6 times) faster than POSIX. The scripts below replace everything between double quotes. the first one is written in POSIX, another in PCRE.
Regular Expression Explanation:
| “ | a quote, followed by … |
| [ | a character class … |
| ^ | that isn't … |
| " | another quote … |
| ] | the end of the character class … |
| * | zero or more times … |
| “ | another quote appears. |
Source Code:
<html>
<head><title>Using POSIX</title></head>
<body>
<form action="<?php $_SERVER['PHP_SELF'] ?>"
method="post">
<input type="text" name="value"
value="<? print stripslashes($_POST['value']); ?>"/>
<br/>
<input type="submit" value="Submit" /><br/><br/>
<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" )
{
$mystr = $_POST['value'];
$mynewstr = ereg_replace(
'"[^"]*"', '"***"', $mystr );
print stripslashes($mynewstr);
}
?>
</form>
</body>
</html>
<html>
<head><title>Using PCRE</title></head>
<body>
<form action="<?php $_SERVER['PHP_SELF'] ?>"
method="post">
<input type="text" name="value"
value="<? print stripslashes($_POST['value']); ?>"/>
<br/>
<input type="submit" value="Submit" /><br/><br/>
<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" )
{
$mystr = $_POST['value'];
$mynewstr = preg_replace(
'/"[^"]*"/', '"***"', $mystr );
print stripslashes($mynewstr);
}
?>
</form>
</body>
</html>
Similar Posts
- None Found


































