Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php find string

how to find if this string :

132,139,150,166,176

is in this one? :

132,139,150,166,176,131,140,151,165,175
like image 294
Ste Avatar asked Nov 22 '10 16:11

Ste


People also ask

How can I find a specific string in PHP?

Answer: Use the PHP strpos() Function You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

How do I find a string in a string?

Run a loop from start to end and for every index in the given string check whether the sub-string can be formed from that index. This can be done by running a nested loop traversing the given string and in that loop running another loop checking for sub-strings starting from every index.

How do I check if a string contains something?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.

How check string is value or not in PHP?

The is_string() function checks whether a variable is of type string or not. This function returns true (1) if the variable is of type string, otherwise it returns false/nothing.


1 Answers

You can use strpos function to find the occurrence of one string within another.

$str1 = '132,139,150,166,176,131,140,151,165,175';
$str2 = '132,139,150,166,176';

if( strpos($str1,$str2) !== false) {
   // $str2 exists within $str1.
}

Note that strpos will return 0 if $str2 is found at the beginning of $str1 which in fact is the case above and will return false if not found anywhere.

You must use the identity operator !== which checks both value and type to compare the return value with false because:

0 !== false is true 

where as

0 != false is false 
like image 132
codaddict Avatar answered Sep 22 '22 10:09

codaddict