2012-09-06 65 views

回答

3

试试这个代码:

$pos = strrpos($mystring, "7"); 
if ($pos === false) { // note: three equal signs 
    // not found... 
} 
else{ 
    //string found 
} 
+0

注意 'strrpos的' find最后一次出现,而不是“strpos”,其找到第一个外观。任何功能都将用于此用例。 – Omn

2

看一看strpos,你可以用它来寻找其中的子字符串中的出现(以及由此延伸,是否它发生)。参见如何正确地做检查的第一个例子。

0
if(stristr('3275', '7') !== false) 
{ 
    // found 
} 
+0

'如果(stristr( “3275”, “7”)!== FALSE){ // 发现 }' – rynhe

1

http://us3.php.net/strpos

int strpos (string $haystack , mixed $needle [, int $offset = 0 ]) 

查找日针在草堆串中第一次出现电子数字位置。

确保您使用“==假的!”比较返回值时,看它是否存在(否则7325将返回位置0和0 ==虚假) - !===和==是比较值和类型(布尔VS整数)

2

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"; 
} 
0

尝试这样

$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...!!!'; 
相关问题