Forum Moderators: coopster

Message Too Old, No Replies

Quick regular expression

         

ramoneguru

7:37 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



I have a string and I need to check and see if there is an # sign in it. Is there a regular expression that will do that for me?
--Nick

Birdman

7:42 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you only need to know whether the character is present in the string, then this will do it for you:

if ( strpos($string, '#')!== false ) {
...do stuff, cause # is in there...
} else {
...it's not there...
}

Cheers

ramoneguru

7:56 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



Cool thanks.

I was just playing around and found that this worked as well:
$namesBuffer = "lalal # 1234 # haahha";
$result = preg_match("/#/", $namesBuffer);

Yeah.......so that was a little bit more easy to come up with than I thought....

--Nick

coopster

8:00 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



strpos will run much faster though, if you don't need to use the regular expression engine, don't -- for performance reasons.