可能重复:
How can I check if a word is contained in another string using PHP?PHP字符串如何找到一串数字一个数字
我希望有一些PHP代码来检查,如果某个号码出现在一个字符串数字的,因此,例如我该如何检查,如果7号出现在数3275?
我已经试过STRCMP但我不能工作,这一个:(
可能重复:
How can I check if a word is contained in another string using PHP?PHP字符串如何找到一串数字一个数字
我希望有一些PHP代码来检查,如果某个号码出现在一个字符串数字的,因此,例如我该如何检查,如果7号出现在数3275?
我已经试过STRCMP但我不能工作,这一个:(
试试这个代码:
$pos = strrpos($mystring, "7");
if ($pos === false) { // note: three equal signs
// not found...
}
else{
//string found
}
看一看strpos
,你可以用它来寻找其中的子字符串中的出现(以及由此延伸,是否它发生)。参见如何正确地做检查的第一个例子。
if(stristr('3275', '7') !== false)
{
// found
}
'如果(stristr( “3275”, “7”)!== FALSE){ // 发现 }' – rynhe
int strpos (string $haystack , mixed $needle [, int $offset = 0 ])
查找日针在草堆串中第一次出现电子数字位置。
确保您使用“==假的!”比较返回值时,看它是否存在(否则7325将返回位置0和0 ==虚假) - !===和==是比较值和类型(布尔VS整数)
strpos()是你的朋友,PHP不是强类型的,所以你可以考虑数字为字符串。
$mystring = 3232327;
$findme = 7;
$pos = strpos($mystring, $findme);
if ($pos === false) {
echo "The number '$findme' was not found in the number '$mystring'";
} else {
echo "The number '$findme' was found in the number '$mystring'";
echo " and exists at position $pos";
}
尝试这样
$phno = 1234567890;
$collect = 4;
$position = strpos($phno, $collect);
if ($position)
echo 'The number is found at the position'.$position;
else
echo 'Sorry the number is not found...!!!';
注意 'strrpos的' find最后一次出现,而不是“strpos”,其找到第一个外观。任何功能都将用于此用例。 – Omn