0

This is what I have:

$a = '3,24,57';

if (strpos($a, '7') == true) {
    echo 'number found';
}

The code will return "number found" because of the number 57 but there is no number 7 in the string. How can I make this work in a way that will return true only if the string is like this: "3,7,24,57"

Thanks

Pedro
  • 13
  • 3

3 Answers3

2

Just try like this.

$array = explode(",", $a);
if (in_array("7", $array )) {
    echo 'number found';
}
Web Star
  • 431
  • 3
  • 20
0

Try this:

$a = '3,24,57';

if (strpos($a, ',7,') == true || strpos($a, '7,') == true || strpos($a, ',7') == true) {
    echo 'number found';
}
Alan Yu
  • 1,462
  • 12
  • 21
0

In your case, use the following regex: Like this:

<?php

  $a = '3,24,57';
  $find = 7;
  if (preg_match("/(?<!\d){$find}(?!\d)/", $a)) {
    echo "number found";
  } else {
    echo "number not found";
  }    

?>
Petr Fořt Fru-Fru
  • 858
  • 2
  • 8
  • 23