Page 1 of 1

preg_replace Errors

Posted: Thu May 17, 2007 5:40 pm
by JustinMs66
its displaying errors such as:
Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 0 in functions.php

and i figure its because some of the charictars, preg_repace is trying to use as modifiers.
how would i fix this?

Code: Select all

function removebadchars($str)
{
	$pats1 = "~.!.@.#.$.%.^.&.*.+.`.-.=.[.].\.'.;.,.{.}.|.:.<.>.?";
	$ex1 = explode(".", $pats1);
	
	foreach($ex1 as $patsb)
	{
		$pats[] = "/".$patsb."/";
		$reps[] = "";
	}
	
	return preg_replace($pats, $reps, $str);
}

Posted: Thu May 17, 2007 6:08 pm
by georgeoc
Would this help? preg_quote

Posted: Thu May 17, 2007 8:28 pm
by feyd
Here's the only pattern you'll need.

Code: Select all

_[~!@#\\$%^&\\*\\+`\\-=\\[\\]\\\\';,{}|<>\\?]_