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.