php find string
Asked Answered
M

2

6

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
Michelsen answered 22/11, 2010 at 16:25 Comment(2)
To raise the chance people will help you, it's advised to accept some answers of your previous questions first.Build
Just wondering, why would this specific pair of strings be any more special than any two random strings of data?Arterio
S
2

You want strpos

strrpos("132,139,150,166,176,131,140,151,165,175","132,139,150,166,176");

If a string exists, its starting point will be returned from zero onward, if it is not present the result will be 'false'

Silsbye answered 22/11, 2010 at 16:26 Comment(1)
The OP seems to be new to PHP. You should clarify that he should use an identity operator over an equality operator when using strrpos, to avoid pitfalls.Sway
S
13

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 
Setiform answered 22/11, 2010 at 16:26 Comment(0)
S
2

You want strpos

strrpos("132,139,150,166,176,131,140,151,165,175","132,139,150,166,176");

If a string exists, its starting point will be returned from zero onward, if it is not present the result will be 'false'

Silsbye answered 22/11, 2010 at 16:26 Comment(1)
The OP seems to be new to PHP. You should clarify that he should use an identity operator over an equality operator when using strrpos, to avoid pitfalls.Sway

© 2022 - 2024 — McMap. All rights reserved.