Which Method Is Preferred Strstr or Strpos

Which method is preferred strstr or strpos?

From the PHP online manual:

If you only want to determine if a
particular needle occurs within
haystack, use the faster and less
memory intensive function strpos()
instead.

PHP stristr versus stripos

0 doesn't equal false if you use === (or !==).

See this fiddle for proof: http://phpfiddle.org/main/code/nih-esg

More info on the PHP site here: http://www.php.net/manual/en/language.operators.comparison.php

preg_match() vs strpos() for match finding?

I would prefer the strpos over preg_match, because regexes are generally more expensive to execute.

According to the official php docs for preg_match:

Do not use preg_match() if you only
want to check if one string is
contained in another string. Use
strpos() or strstr() instead as they
will be faster.

What is the fastest way to find the occurrence of a string in another string?

strpos seems to be in the lead, I've tested it with finding some strings in 'The quick brown fox jumps over the lazy dog':

  • strstr used 0.48487210273743 seconds for 1000000 iterations finding 'quick'
  • strpos used 0.40836095809937 seconds for 1000000 iterations finding 'quick'
  • strstr used 0.45261287689209 seconds for 1000000 iterations finding 'dog'
  • strpos used 0.39890813827515 seconds for 1000000 iterations finding 'dog'
<?php

$haystack = 'The quick brown fox jumps over the lazy dog';

$needle = 'quick';

$iter = 1000000;

$start = microtime(true);
for ($i = 0; $i < $iter; $i++) {
strstr($haystack, $needle);
}
$duration = microtime(true) - $start;
echo "<br/>strstr used $duration microseconds for $iter iterations finding 'quick' in 'The quick brown fox jumps over the lazy dog'";

$start = microtime(true);
for ($i = 0; $i < $iter; $i++) {
strpos($haystack, $needle);
}
$duration = microtime(true) - $start;
echo "<br/>strpos used $duration microseconds for $iter iterations finding 'quick' in 'The quick brown fox jumps over the lazy dog'";

$needle = 'dog';

$start = microtime(true);
for ($i = 0; $i < $iter; $i++) {
strstr($haystack, $needle);
}
$duration = microtime(true) - $start;
echo "<br/>strstr used $duration microseconds for $iter iterations finding 'dog' in 'The quick brown fox jumps over the lazy dog'";

$start = microtime(true);
for ($i = 0; $i < $iter; $i++) {
strpos($haystack, $needle);
}
$duration = microtime(true) - $start;
echo "<br/>strpos used $duration microseconds for $iter iterations finding 'dog' in 'The quick brown fox jumps over the lazy dog'";

?>

Match exactly with strstr

Ehmm, just compare?

if ('www.mydomain.com/greenapples' === $myurl) {
echo 'green apples!';
}

update

Without further info, I'm not sure if this fits your question, but if you're only interested in the last part of the URL and take into account the possibility that the URL contains a query-string (e.g. ?foo=bar&bar=foo), try something like this:

// NOTE: $myurl should be INCLUDING 'http://'
$urlPath = parse_url($myurl, PHP_URL_PATH);

// split the elements of the URL
$parts = explode('/', $urlPath);

// get the last 'element' of the path
$lastPart = end($parts);

switch($lastPart) {
case 'greenapples':
echo 'green!';
break;

case 'greenapplesandpears':
echo 'green apples AND pears!';
break;

default:
echo 'Unknown fruit family discovered!';

}

Documentation:

http://www.php.net/manual/en/function.parse-url.php

http://php.net/manual/en/function.end.php

http://php.net/manual/en/control-structures.switch.php

Strpos always gives true

Try this

if (strpos($links, 'http') === false) {
$linkai = 'http://'.$links;
}

In strpos documentation says return value not Boolean always.

"Warning
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function."

PHP Multiple 'stripos' statements

You can easily condense this as such;

if(
stripos($name, "Name", true) &&
(stripos($name, "first", true)) || (stripos($name, "for", true)) || (stripos($name, "1", true)) &&
stripos($name, "error")
)
{
/* Your code */
}

You could also do the following which would work better (IMO);

if(
stristr($name, "Name") &&
(stristr($name, "first") || stristr($name, "for") || stristr($name, "1")) &&
stristr($name, "error")
)
{
/* Your code */
}

Why strpos fail in this example

Because it equals 0 which equates to false.

Instead use:

if(strpos($txt, 'http://www.')!==false){
echo 'true strpos'; //shows
}

Search php string - %like% sql

if(stristr('monkey', $string) && stristr('66', $string)) {
//Do stuff
}


Related Topics



Leave a reply



Submit