note 42787 added to pcre.pattern.syntax
| From: | php-general at lists dot php dot net | Date: | Sat, 29 May 2004 06:15:22 +0000 |
| Subject: | note 42787 added to pcre.pattern.syntax | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-70268@lists.php.net to get a copy of this message | ||
In addition to the meta-characters mentioned above, there can be another special character in a
regular expression: the delimiter you use to start and end your expression. Often people use the /
character for this.
For example, if you wanted to search for text surrounded by opening and closing tags
like'<TD>SELL</TD>' and replace it with nothing (erase it), you might be
tempted to use a regex like this:
<?php
$myNewText = preg_replace('/<TD>SELL</TD>/', "", $myText);
?>
This does not work properly. As mentioned in the Introduction at the top of http://www.php.net/manual/en/ref.pcre.php, if
the delimiter appears in the middle of your regular expression, then you must put a \ character
before it. So this DOES work:
<?php
$myNewText = preg_replace('/<TD>SELL<\/TD>/', "", $myText);
?>
That same Introduction also mentions that you can start and end your expression with characters
other than the usual /. Because there are no % characters in the middle of my expression above, I
might prefer to use the following:
<?php
$myNewText = preg_replace('%<TD>SELL</TD>%', "", $myText);
?>
That also works correctly, and I did not need a \ before the /.
----
Manual Page -- http://www.php.net/manual/en/pcre.pattern.syntax.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42787
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42787&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42787&report=yes
Search -- http://master.php.net/manage/user-notes.php