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
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 .
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With