检查字符串是否有特定数字,而不是数字

Check if string has a specific number, not digit

这是我的:

$a = '3,24,57';

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

代码将 return “找到数字”,因为数字 57 但字符串中没有数字 7。只有当字符串是这样的:“3,7,24,57”

时,我怎样才能使 return 为真

谢谢

就这样试试吧

$array = explode(",", $a);
if (in_array("7", $array )) {
    echo 'number found';
}

试试这个:

$a = '3,24,57';

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

对于您的情况,请使用以下正则表达式: 像这样:

<?php

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

?>