Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which method is preferred strstr or strpos?

Tags:

php

strpos

strstr

I noticed a lot of developers are using both strstr and strpos to check for a substring existence. Is one of them preferred and why ?

like image 231
johnlemon Avatar asked Apr 28 '11 14:04

johnlemon


People also ask

What is the strpos () function used for?

strpos in PHP is a built-in function. Its use is to find the first occurrence of a substring in a string or a string inside another string. The function returns an integer value which is the index of the first occurrence of the string.

What is the difference between Strpos and Stripos?

The strpos() function finds the position of the first occurrence of a string inside another string. The stripos() function finds the position of the first occurrence of a string inside another string. 2. It is case-sensitive function.

What is Strstr PHP?

The strstr() function searches for the first occurrence of a string inside another string. Note: This function is binary-safe. Note: This function is case-sensitive. For a case-insensitive search, use stristr() function.


1 Answers

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.

like image 82
Alnitak Avatar answered Oct 23 '22 10:10

Alnitak