我想知道如果该 ip 地址具有 x 字符串,我如何检查我的字符串(特别是一个 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.
Run Code Online (Sandbox Code Playgroud)
那么如何检查 $ip 如果它有 $x 的字符串呢?
如果您很难理解这种情况,请发表评论。
谢谢你。
您还可以使用strpos(), 如果您专门查找字符串的开头(如您的示例所示):
if (strpos($ip, $x) === 0)
Run Code Online (Sandbox Code Playgroud)
或者,如果您只想查看它是否在字符串中(而不关心它在字符串中的位置):
if (strpos($ip, $x) !== false)
Run Code Online (Sandbox Code Playgroud)
或者,如果您想比较开始的 n 个字符,请使用strncmp()
if (strncmp($ip, $x, strlen($x)) === 0) {
// $ip's beginning characters match $x
}
Run Code Online (Sandbox Code Playgroud)