How to be a better php programmer 1 comparing strpos preg_match and stripos

How to be a better php programmer   1   comparing strpos preg_match and stripos


Lets look at the speed of some php
functions I will call strpos, preg_match, and stripos.
Let’s find out which is the fastest when wanting to obtain a similar goal.
– – – – – – – – – – – – – – – – – – – – – – – – – strpos is a php function that looks for
a string within another string. If the function finds a match it will
return the position of where it was found. If the string you are searching
for does not exist within the other string then you will get a return value of false.
Notice you can get a number or a boolean. – – – – – – – – – – – – – – – – – – – – – – – – –
If you are testing to determine if the string you are searching for is
within another string, you will want to use an Identical Comparison Operator.
– – – – – – – – – – – – – – – – – – – – – – – – – preg_match is a function that looks for
a regular expression within another string. – – – – – – – – – – – – – – – – – – – – – – – – –
stripos is similar to strpos. The difference is that it has an ‘i’ that stands for
case-insensitive. – – – – – – – – – – – – – – – – – – – – – – – – –
The function is not sensitive to case so if you are searching
for the lower case letter ‘a’ it will match
on the upper case letter ‘A’. – – – – – – – – – – – – – – – – – – – – – – – – –
While searching for a 2 character string within a 24 character random string,
1 million times, the results of the speed test conclude that
your program will run faster if you tend to use strpos whenever
you can. This agrees with what php.net states:
“use the faster and less memory intensive function strpos()”

Leave a Reply

Your email address will not be published. Required fields are marked *