【发布时间】:2012-06-23 17:31:46
【问题描述】:
我想知道如果该IP地址具有x字符串,我如何检查I字符串(特别是IP地址)
例如
$ip = "66.124.61.23" // ip of the current user
$x = "66.124" // this is the string what I want to check in my $ip.
那么如何检查 $ip 是否有 $x 字符串?
如果您很难理解这种情况,请发表评论。
谢谢。
【问题讨论】:
我想知道如果该IP地址具有x字符串,我如何检查I字符串(特别是IP地址)
例如
$ip = "66.124.61.23" // ip of the current user
$x = "66.124" // this is the string what I want to check in my $ip.
那么如何检查 $ip 是否有 $x 字符串?
如果您很难理解这种情况,请发表评论。
谢谢。
【问题讨论】:
$email = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
根据$domain判断是否找到字符串(如果domain为null,则找不到字符串)
此函数区分大小写。对于不区分大小写的搜索,请使用stristr()。
您也可以使用 strpos()。
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
另请阅读之前的帖子,How can I check if a word is contained in another string using PHP?
【讨论】:
使用strpos()。
if(strpos($ip, $x) !== false){
//dostuff
}
注意使用双等号以避免类型转换。 strpos 可以返回 0(在您的示例中将返回),这将使用单个等号评估为 false。
【讨论】: