Conditional Comma-Separated Variables

I want to match numbers from a comma-separated string - bare in mind - if the value "1" is found the following statement should evaluate to true - but it doesn't. I would also like to make it so if the value "15" is found (and the functions searches for "1"), "15" will not return true just because there's a "1" in there. Here's what I've come up with... but, it doesn't work. (attached)
<?php 
$var = "1,2,3,15"; 
$explode = explode(",",$var); 
if (preg_match("1", $explode)) { 
 echo "A match was found."; 
} else { 
 echo "No match was found."; 
} 
?>

Open in new window

LVL 14
EMB01Asked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
<?php
$var = "1,2,3,15";
$explode = explode(",",$var);
if ( in_array(1,$explode) ) {
 echo "A match was found.";
} else {
 echo "No match was found.";
}
?>
0
 
hieloConnect With a Mentor Commented:
alternatively:
<?php
$var = "1,2,3,15";

if (FALSE === strpos( ",".$var.",", "1") ) {
 echo "No match was found.";
} else {
 echo "A match was found.";
}
?>
0
 
EMB01Author Commented:
Thanks, I am quite unfamiliar with the in_array function and seldom use the numerious string functions... still learning. Thanks, again!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.