How to find an element in a string?

Zado
Zado used Ask the Experts™
on
Hi Experts!

I've got the following string:
$str='10,11,26,29,50';
and I want to check if element '2' exists in this string, so I want to compare '2' with each number from this string (10,11,26,29,50), so in this case the result should be 'false'.
How to do this? Strpos or Strstr don't work as expected and show 'true'.

Thanks for any help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Without having to explode this string, you could cheat a little bit, add a comma at the start and end of the string, and then search for ',2,'

You don't even have to add it to the string, you could prepend and append a comma in the Strpos function, similar to the following:

$mystring = '10,11,26,29,50';
$findme   = ',2,';
$pos = strpos(','.$mystring.',', $findme);
if ($pos !== false) {
     echo "The string '$findme' was found in the string '$mystring'";
         echo " and exists at position $pos";
} else {
     echo "The string '$findme' was not found in the string '$mystring'";
}

Open in new window

Author

Commented:
I thought about it, but the string may contain only one element: $str='27'; Then your script wouldn't work.
The script would still work - why wouldn't it?  it would just search for ,2, in ,27, and would return false.  I presume that's the intended result you would want, based on your original query.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Is there any condition like not exploding the string?

easy way:

$mystring = '10,11,26,29,50';
$findme = '2';
$pattern = ',';
$flag = 0;
$myarray = split($pattern,$mystring) ;
foreach ($myarray as &$value) {
	if ($value == $findme) {
		$flag=1;
	}		
}

if ($flag == 1) {
	echo "$findme was found in string $mystring!<br/>";
} else {
	echo "no luck :(";
}

Open in new window

Author

Commented:
Sorry LesterClayton, I meant string=2 and then you're looking for ',2,', so I wouldn't say It will work, but it works, so thank you for your help :-)

Asavah, your script works too, thank you guys!
Most Valuable Expert 2011
Top Expert 2016

Commented:
How long is the string?  Just explode() it!  It's easier that way.

Author

Commented:
It may contain one element or even 100 elements. Thanks.
Now you have to choose which script you want to accept :D

The benefit of doing it "my way" is that there is no iteration or loop required, therefore executing faster.  The other methods suggested are good and "better practice" than mine, but will not return the position of the found item in the string.

I would also suggest a change to my code, new code as follows.

$mystring = '10,11,26,29,50';
$findme   = '2';
$pos = strpos(','.$mystring.',', ','.$findme.',');
if ($pos !== false) {
     echo "The string '$findme' was found in the string '$mystring'";
         echo " and exists at position $pos";
} else {
     echo "The string '$findme' was not found in the string '$mystring'";
}

Open in new window

Author

Commented:
Thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial