Jump to content

Using Preg_Replace


alexnofue

Recommended Posts

Hi, i'm trying to use preg_replace to clean a variable value if it isn't what i need let's say i need a number in a variable, so i have: preg_replace('/[^0-9]/', '', $var); so if $var = 'dog' then i get '';if $var = '12' then i get '12' now i want to do the same with some values, e.g. i only want to receive 'dog' and 'cat' after replacing so if $var = 'bird' then i would get ''if $var = 'dog' then i would get 'dog' i have so far preg_replace('/dog|cat/i', '', $var); and didn't work, can i do this using preg_replace?or should i do it some other way?

Link to comment
Share on other sites

Your approach with the numbers is "replace all non numeric characters with an empty string", and your current approach with the string is "replace the string dog or the string cat with an empty string". Since you're attempting to white list those rather than blacklist them, you'll need to negate the expression. Perhaps:

preg_replace('/[^(dog)(cat)]/i', '', $var);

or

preg_replace('/(?!(dog|cat))/i', '', $var);

Either of those should leave intact "dog", "dogdog", "dogcat", etc. and turn "birddog" into "dog" and "bird eaten by a cat chased by a dog" into "catdog", etc.

Link to comment
Share on other sites

Are you sure this does what you think it does? [^(dog)(cat)]I thought square brackets only had individual characters in them, the parentheses would be considered characters, something equivalent to this: [^()acdgot]

Link to comment
Share on other sites

You're right, i'm trying to do a white list. i tried

preg_replace('/[^(dog)(cat)]/i', '', 'bird');

and got 'd' then i tried

preg_replace('/(?!(dog|cat))/i', '', 'bird');

and got 'bird'. I was somewhere close to this one with no results

Link to comment
Share on other sites

I believe that the expression ?!(dog|cat) refers to any text that is not followed by "dog" or "cat". What you're going to have to do is extract from a string all the words you're looking for without regular expressions.I put this together but there might be a more efficient way to do it:

echo get_words( array('dog', 'cat'), 'A dog, a bird and a catterpillar' ); function get_words($search, $str) {  $newStr = '';  $currentPos = 0;   while(1) {	$find = array();	foreach($search as $word) {	  if($s = strpos($str, $word, $currentPos) !== false) {		$find[$word] = $s;	  }	} 	if(count($find) == 0) {	  break;	} 	asort($find); 	foreach($find as $word=>$pos) {	  $len = strlen($word);	  $newStr .= substr($str, $pos, $len );	  $currentPos = $pos + $len;	}  }  return $newStr;}

Link to comment
Share on other sites

Hi, thanks ingolme for your response i found this page with some info that seems to work after i "change" it to php. wiki.tcl.tk/989 got it like this

preg_replace('/^((?!(dog|cat)).*)|^((dog|cat).+)/i', '', 'bird');

not quite sure how does it work, wich could mean trouble later on.

Link to comment
Share on other sites

Reading the expression you're showing, I deduce that it should match anything that starts with dog or cat and has at least one more character, and also matches anything that doesn't start with dog or cat. I don't think there's a case it doesn't match. I'll have to do tests on it to make sure. Edit: Of course, this expression matches any string except "dog" and "cat". That means that the string "dogx" or "acat" will be allowed through.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...