• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

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

0
EMB01
Asked:
EMB01
  • 2
2 Solutions
 
hieloCommented:
<?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
 
hieloCommented:
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now