Forum Moderators: coopster

Message Too Old, No Replies

strpos vs preg_match

         

Code Sentinel

9:48 pm on Jul 4, 2005 (gmt 0)

10+ Year Member



I know strpos is supposed to be faster, I'm writing a script with a decent amount of "prechecking" the data before processing and realized I've been using mainly preg_match. (just learned regular expressions and now wonder why I didn't bother learning this stuff sooner)

I would like to change some of these to strpos but I'm at a loss how I'm supposed to enforce matches at start and end of string (the ^ and $), I don't want strpos to match something in the middle of a string.

ex:

preg_match('/^widget 2\.0/i', $string)

This string must *start* with this pattern to be able to evaluate it as true(in switch statements).

vs

strpos($string, 'widget 2.0')

which would match(or rather not return false) even if the string was "blue widget 2.0"

I guess the short of it is I'm wondering if there's a way to match the meaning of ^ or $ with strpos.

coopster

2:09 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



strpos [php.net] has an optional offset parameter which allows you to specify at which character in the string to start searching. There is no end anchor though. You are correct in that if you need to specify starting and ending anchors you are going to need the power of a regular expression engine.