preg_replace Errors

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

preg_replace Errors

Post 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);
}
georgeoc
Forum Contributor
Posts: 166
Joined: Wed Aug 09, 2006 4:21 pm
Location: London, UK

Post by georgeoc »

Would this help? preg_quote
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Here's the only pattern you'll need.

Code: Select all

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