Page 1 of 1

expression query

Posted: Thu Jan 22, 2009 11:22 am
by rsmarsha
I have the following expression but it doesn't seem to strip out what it should.

Does this look correct? I want it to strip all but the characters in the first part of the preg_replace statement, so it should strip any of those characters from the whole string.

Code: Select all

 
$hrow[$k] = preg_replace("/[^ +=.,'&\-(){}£€\/a-z0-9\d]/i", "", $hrow[$k]);
 
After reading the posts here It looks like I should remove the \d as that's for a single digit.

Code: Select all

 
$hrow[$k] = preg_replace("/[^ +=.,'&\-(){}£€\/a-z0-9]/i", "", $hrow[$k]);
 
So does that look right? I'm not sure if i need a * at the end.

Sorry for the simple question, if someone could check it through for me that would be much appreciated. :)

Re: expression query

Posted: Thu Jan 22, 2009 11:37 am
by mintedjo
I don't know whats wrong - it looks like it works to me.
And for future reference

Code: Select all

\d
is another way of saying

Code: Select all

[0-9]
If you explain whats going wrong in more detail I might be able to help.

Re: expression query

Posted: Thu Jan 22, 2009 11:59 am
by prometheuzz
rsmarsha wrote:I have the following expression but it doesn't seem to strip out what it should.

Does this look correct? I want it to strip all but the characters in the first part of the preg_replace statement, so it should strip any of those characters from the whole string.
Your two statements "I want it to strip all but the characters" and "so it should strip any of those characters" contradict each other. Perhaps you meant to say "so it shouldn't strip any of those characters"?
rsmarsha wrote:...
So does that look right? I'm not sure if i need a * at the end.
...
No idea. Try posting an example string and explain what output you get and how this output does not meet your expectations.

Re: expression query

Posted: Sat Jan 24, 2009 2:02 am
by aschlosberg
You can leave the \d in place as it will produce the same results as 0-9.

There are certain characters that need to be escaped (like you have with the \/ - meaning a literal / and not an end of regex pattern /). The + and the . each have a meaning so I think you are currently matching one or more spaces ( +) and anything (.).

Code: Select all

$hrow[$k] = preg_replace("/[^ \+=\.,'&\-(){}£€\/a-z\d]/i", "", $hrow[$k]);
Give this a try and see if the results are what you want. Here is a good cheat sheet with a section on the meta characters that have to be escaped - http://www.addedbytes.com/cheat-sheets/ ... eat-sheet/

Re: expression query

Posted: Sat Jan 24, 2009 2:14 am
by prometheuzz
aschlosberg wrote:... Here is a good cheat sheet with a section on the meta characters that have to be escaped - http://www.addedbytes.com/cheat-sheets/ ... eat-sheet/
That sheet is not entirely correct: the '<' and '>' characters are not meta characters. Yes, sometimes they need to be escaped, but the hyphen also needs escaping in certain cases (inside a character set) but that is also not a meta character.

Re: expression query

Posted: Sat Jan 24, 2009 11:51 pm
by aschlosberg
prometheuzz wrote:the '<' and '>' characters are not meta characters. Yes, sometimes they need to be escaped
Could you please elaborate... I'm no regex expert and would like to improve.

Re: expression query

Posted: Sun Jan 25, 2009 1:09 am
by prometheuzz
aschlosberg wrote:
prometheuzz wrote:the '<' and '>' characters are not meta characters. Yes, sometimes they need to be escaped
Could you please elaborate... I'm no regex expert and would like to improve.
Sure.
Say you want to match opening tags of any kind. You can do that like this:

Code: Select all

<?php
$text = "text <b> text <code> text";
if(preg_match_all('/<.*?>/', $text, $matches)) {
  print_r($matches);
}
?>
and as you can see, there is no need to escape either '<' or '>'.