Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中只获取文件的一部分?_Php_Variables_If Statement - Fatal编程技术网

在PHP中只获取文件的一部分?

在PHP中只获取文件的一部分?,php,variables,if-statement,Php,Variables,If Statement,在互联网上找不到关于这个的任何信息,或者 基本示例: 我想知道的一个很好的例子是,如果在一个句子中找到一个单词或短语,,您将如何创建一个if语句来返回true 另一个例子: 假设我们在一个外部文件中有一个IP阻止列表。因此,我假设我们需要在if语句的某个地方使用file\u get\u contents // IP Blocklist 118.92.00 119.92.11 125.23.10 好的,这是我们的示例IP阻止列表。您如何创建一个能够找到中间IP(119.92.11)的if语句,即

在互联网上找不到关于这个的任何信息,或者

基本示例: 我想知道的一个很好的例子是,如果在一个句子中找到一个单词或短语,,您将如何创建一个if语句来返回true

另一个例子: 假设我们在一个外部文件中有一个IP阻止列表。因此,我假设我们需要在if语句的某个地方使用
file\u get\u contents

// IP Blocklist
118.92.00
119.92.11
125.23.10

好的,这是我们的示例IP阻止列表。您如何创建一个能够找到中间IP(119.92.11)的if语句,即使其中有其他内容(请记住它很可能会更改!)?

这是用于外部文件的

$ips = file ( $file );
$searchIP = "119.92.11";
$found = false;
foreach ( $ips as $ip ) {
    if ($ip == $searchIP) {
        $found = true;
    }
}

if ($found) {
    echo $searchIP, " Found";
}

只需使用strpos函数

函数的作用是:返回 另一根线中的一根线

如果未找到字符串,此函数将返回FALSE

例如:

$ipAddresses = '// IP Blocklist
118.92.00
119.92.11
125.23.10';

if (strpos($ipAddresses,"119.92.11") != FALSE) {
    // IP ADDRESS WAS FOUND
} else {
    // IP ADDRESS NOT FOUND
}

您的两个示例需要两种不同的技术才能可靠

示例1仅要求:

如果希望以不区分大小写的方式进行匹配,可以使用
stripos()

例如,使用数组会更好。这是因为如果
11.22.22.110
在数组中,则
strpos()
方法将匹配
11.22.22.11
,而您不希望这样

相反,您可以这样做,使用:


我会使用正则表达式来保证准确性和灵活性:

$lines = file($blockListFile);
$findIp = '119.92.11';

$findIp = trim($findIp, '.');

// The number of unspecified IP classes (e.g. for "192.92.11", it would be 1,
// but for "192.92" it would be 2, and so on).
$n = 4 - (substr_count($findIp, '.') + 1)

foreach ($lines as $line) {
    if (preg_match('/^' . preg_quote($findIp, '/') . '(\.\d{1,3}){0,' . $n . '}$/', $line)) {
        // the line matches the search address
    } else {
        // the line does not match the search address
    }
}
此方法允许搜索任意数量的IP类(例如“192.92.11.45”、“192.92.11”、“192.92”,甚至只是“192”)。它将始终在行首匹配,因此,例如,搜索“192.92.11”将与“24.192.92.11”不匹配。它还只匹配完整类,因此搜索“192.92.11”将不会匹配“192.92.115”或“192.92.117.21”

编辑:

请注意,此解决方案假定:

  • 您的搜索词是在完整类中指定的(例如,搜索“192.92.11”意味着您希望匹配
    /^192.92.11(\.\d{1,3})?$/
  • 在阻止列表文件中指定的IP也在完整类中指定

是您所需要的一切-
如果(strpos($subjectString,$searchString)!==FALSE){//substring exists}
一如既往地完美Baba:)。不过,老实说,这种方法似乎不像其他人建议的strpos方法那样干净。你认为哪种方法最好?你提出的问题完全是两码事。。我将
strpos
用于字符串,但不用于文件不。。。文件可能有太多您想要自定义的内容。。对于每个大文件,这两种方法都会失败,只有
fopen
最有效。。正如我所说。。这取决于你的使用情况啊,好的。好吧,最多我想要的目标文件中不会有超过2个不同的URL,所以它不会太大?只需给出一个完整的描述,你到底想做什么和存储我会告诉你最好的解决方案,你需要使用
==
或者当“119.92.11”位于位置0(文件的开头)时,这将给出一个假阴性。此外,我不会
strpos()
整个文件内容,而是逐行显示。如果像“24.119.92.11”这样的IP在阻止列表中,会发生什么情况?它将给出另一个假阳性,因为OP大概在寻找“119.92.11.*”。它还将为诸如“119.92.111”、“119.92.115”等地址提供误报。在这种情况下,最好使用正则表达式。对不起,我不是故意挑你的毛病。:-)ye,你现在应该纠正所有其他答案中完全相同的评论,这些答案使用了作为示例的完全相同的概念,即如何使用strpos而不是为他做实际工作。嘿,Dave。很好的回答:)。不过,如果可以的话,还有一个简单的问题:第二个示例如何在ips.txt的
列表中找到某个IP?代码假定您已将要查找的IP分配给
$searchIP
。因此,您正在检查
$searchIP
中的值是否存在于数组
$ips
中,该数组是第一行从file.Perfect的内容创建的。很好的回答再好不过了。非常感谢,伙计:)
if (strpos($subjectString, $searchString) !== FALSE) {
  // substring exists
} else {
  // substring doesn't exist
}
// 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
}
$lines = file($blockListFile);
$findIp = '119.92.11';

$findIp = trim($findIp, '.');

// The number of unspecified IP classes (e.g. for "192.92.11", it would be 1,
// but for "192.92" it would be 2, and so on).
$n = 4 - (substr_count($findIp, '.') + 1)

foreach ($lines as $line) {
    if (preg_match('/^' . preg_quote($findIp, '/') . '(\.\d{1,3}){0,' . $n . '}$/', $line)) {
        // the line matches the search address
    } else {
        // the line does not match the search address
    }
}