在互联网上找不到任何关于这个的信息,或者是堆栈溢出?!!?
基本例子:
我想知道的一个很好的例子是,如果在句子中找到一个单词或短语,那么如何创建一个返回true的if语句。
另一个例子是:
假设我们在外部文件中有一个IP块列表。因此,我想我们需要在if语句中的某个地方使用file_get_contents
。
// IP Blocklist
118.92.00
119.92.11
125.23.10
好的,这是我们的例子,IP封锁。您将如何创建一个能够找到中间IP (119.92.11)的if语句,即使其中还有其他内容(记住它很可能会改变!)?
发布于 2012-04-27 12:36:42
您的两个示例需要两种不同的技术才能可靠。
示例1只需要strpos()
if (strpos($subjectString, $searchString) !== FALSE) {
// substring exists
} else {
// substring doesn't exist
}
如果要以不区分大小写的方式进行匹配,则可以使用stripos()
。
例如,两种情况下,最好使用数组。这是因为如果数组中有strpos()
,那么11.22.22.11
方法将与11.22.22.110
匹配--而您不希望这样做。
相反,您可以这样做,使用in_array()
// Get a list of IPs from file and split into an array
$ips = preg_split('/\s+/', trim(file_get_contents('list-of-ips.txt')));
if (in_array($searchIP, $ips)) {
// IP exists
} else {
// IP doesn't exist
}
发布于 2012-04-27 12:31:59
if(strpos($file_contents, "119.92.11") !== false)
{
//do your stuff
}
发布于 2012-04-27 12:32:00
这是用于外部文件的
$ips = file ( $file );
$searchIP = "119.92.11";
$found = false;
foreach ( $ips as $ip ) {
if ($ip == $searchIP) {
$found = true;
}
}
if ($found) {
echo $searchIP, " Found";
}
https://stackoverflow.com/questions/10350595
复制相似问题