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>











































#1 by Amit Verma on January 15, 2009 - 11:04 pm
it would have been better if few more examples with their OUTPUT should have given.
Thanks for the post
Amit Verma
#2 by stuck brain on March 9, 2009 - 9:18 am
Syntax highlight-nya dibenerin dong mas/mbak :), jadi bingung melihat sourcenya.
Padahal saya mengharap mendapat sesuatu yang lebih dengan pembahasan yang mendetil tentang POSIX vs PCRE, tapi ternyata postingnya cuma begini saja :). But, thank you anyway