简体   繁体   中英

Get only part of a file in PHP?

Couldn't find anything about this on the internet, or stackoverflow?!?

Basic Example:

A great example of what I want to know is, how would you create an if statement that returns true if it finds a word or phrase in a sentence .

Another Example:

Let's say we have an IP blocklist in an external file. So I presume we'll need to use file_get_contents somewhere in the if statement.

// IP Blocklist
118.92.00
119.92.11
125.23.10

Ok so that's our example IP blocklist. How would you create an if statement that is able to find the middle IP (119.92.11), even though there is other content there (keeping in mind it could very well change!)?

Your two examples would require two different techniques to be reliable.

Example 1 simply requires strpos() :

if (strpos($subjectString, $searchString) !== FALSE) {
  // substring exists
} else {
  // substring doesn't exist
}

You could use stripos() instead if you want to match in a case-insensitive manner.

For example two it would be better to use an array. This is because the strpos() approach would match 11.22.22.11 if 11.22.22.110 was in the array - and you don't want this.

Instead, you would do something like this, using 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
}
if(strpos($file_contents, "119.92.11") !== false)
{
  //do your stuff
}

This is for external file

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

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

Just use the strpos function.

The strpos() function returns the position of the first occurrence of a string inside another string.

If the string is not found, this function returns FALSE.

For example:

$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
}

I would use a regular expression for accuracy and flexibility:

$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
    }
}

This method allows searching for any number of IP classes (eg "192.92.11.45", "192.92.11", "192.92", or even just "192"). It will always match at the beginning of the line, so that, for example, searching for "192.92.11" will not match "24.192.92.11". It also only matches full classes, so that searching for "192.92.11" will not match "192.92.115" or "192.92.117.21".

Edit:

Please note that this solution assumes:

  • Your search term is specified in full classes (eg searching for "192.92.11" means you want to match /^192.92.11(\\.\\d{1,3})?$/ )
  • The IPs specified in your block list file are also specified in full classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM