Forum Moderators: coopster

Message Too Old, No Replies

preg_replace is throwing an error when I try to find replace "+".

         

erikcw

8:31 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



preg_replace() is throwing the following php error when I try to run the following variables through it:
$find = "+ +";
$replace = "+";

Why is this occuring?


Warning: Compilation failed: nothing to repeat at offset 2 in /home/httpd/vhosts/devdomain.com/httpdocs/inc/functions.inc.php on line 68


$find = strtolower($_POST['find']);
$replace = strtolower($_POST['replace']);
$keywords = trim(strtolower($_POST['keywords']));

if ($_POST['replace_words'] == "Replace Words") {
if ($find!= "" AND $find!= NULL) {

$PATTERNS = "/\b" . $find . "\b/";
$REPLACEMENTS = $replace;
$temp .= preg_replace($PATTERNS, $REPLACEMENTS, $keywords);
// The ABOVE LINE IS LINE 68 in the script

coopster

8:43 pm on Jun 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The plus (+) sign has a special meaning in regular expressions -- it is a 1 or more quantifier for the pattern. If you are truly looking for the plus (+) sign itself, you need to escape the character in your regular expression.

Resources:
Regular Expression Functions (Perl-Compatible) [php.net]
Regular Expressions [webmasterworld.com]
Regular Expressions Tutorial [etext.lib.virginia.edu]