Differences between strpos, strstr and preg_match

5

PHP offers numerous methods to find out if a text string contains another substring:

  • strpos : to find the first occurrence
  • strrpos : to find the last occurrence
  • strstr : to find the first occurrence
  • preg_match : to search using (or not) with regular expressions
  • ...

What are the differences between them (especially strpos and strstr )? Is there one that is faster? Which of them is more advisable or depends on the situation?

    
asked by Alvaro Montoro 22.11.2016 в 15:11
source

2 answers

6

As far as functionality is concerned, it would depend on what you would like to find.

For example, let's look for the element "world" within this chain (you can check that I have twice put the word world in the chain):

$string = "jersiojresoiresmundojrepiojreopsijreposijmundorpeosijreoisj";

And we are going to apply the different methods, which look similar, but are not the same:

strpos

echo strpos($string, "mundo"); //Devuelve 15, la POSICIÓN de la primera ocurrencia

strrpos

echo strrpos($string, "mundo"); //Devuelve 41, la POSICIÓN de la última ocurrencia.

strstr

echo strstr($string, "mundo"); //Devuelve "mundojrepiojreopsijreposijmundorpeosijreoisj", es decir, todo el String desde la primera ocurrencia

preg_match

echo preg_match("/mundo/i", $string); //Devuelve 1 ya que ha encontrado la cadena dentro del string. En caso contrario, marcaría un 0

echo preg_match("/casa/i", $string); //Devuelve 0

As to whether it is faster strpos or strstr , as indicated by the PHP documentation for the case of strstr :

  

If you only want to know if a certain needle appears in a haystack, the strpos () function is used, which is faster and requires less memory.

    
answered by 22.11.2016 / 15:33
source
4
  

PHP Benchmark

Test environment:

  • PHP 7.0.11
  • NGINX 1.11.5
  • iMac 3.4 GHz Intel Core i7 / 16 GB 1600 MHz DDR3 / macOS Sierra 10.12.1

Test function:

$randomString = 'KWFuPHrOoLzcVMVpTFooczSsXvztgORWRYhApKbMvRFLexLOKLLBarQqUpCCPZiqHipTaK';
                                //^^^Foo                          //^^^Bar 
$start = microtime(true);

for ($i = 0; $i < 10000000; $i++) { // iteración 10 milliones

    // Método
}

echo 'Tiempo:  '.number_format(( microtime(true) - $start), 4).' segundos';

Result:

  

Method: strpos ()

  • strpos($randomString, 'Foo'); - Tiempo: 0.5009 segundos
  • strpos($randomString, 'Bar'); - Tiempo: 0.4699 segundos
  

Method: strrpos ()

  • strrpos($randomString, 'Foo'); - Tiempo: 0.7650 segundos
  • strrpos($randomString, 'Bar'); - Tiempo: 0.4894 segundos
  

Method: strstr ()

  • strstr($randomString, 'Foo'); - Tiempo: 1.0125 segundos
  • strstr($randomString, 'Bar'); - Tiempo: 0.9465 segundos
  

Method: preg_match ()

  • preg_match('/Foo/', $randomString); - Tiempo: 1.1475 segundos
  • preg_match('/Bar/', $randomString); - Tiempo: 1.0569 segundos

    in-sensitive string matching:

  • preg_match('/Foo/i', $randomString); - Tiempo: 1.1483 segundos

  • preg_match('/Bar/i', $randomString); - Tiempo: 1.1304 segundos
answered by 22.11.2016 в 16:21