PHP file_get_contents () read _php tutorials for capturing remote file content

Source: Internet
Author: User
Let's take a look at the PHP manual about the file_get_contents () function to read the entire file into a string and file (), unlike the file_get_contents () that reads the files into a string.

The file_get_contents () function is the preferred method for reading the contents of a file into a string

Above is the Help manual is a one-time to read all the content into memory, the following example we will also talk about

_get_contents can read a remote file into a string,
And the separation of the file line is "RN"
Explode to separate strings by "RN"

The code is as follows Copy Code
$shadu _url = "http://localhost/demo.html";
$shadu _str = file_get_contents ($shadu _url);
$line _arr = Explode ("rn", $shadu _str);

The remote request address, the returned processing result information is a string, each field with ' | ' Separated, lines and lines separated by ' RN '
Return content as Id,name,time,email, respectively


Sample Content
1|simaopig|2009-04-01|demo@simaopig.com
2|xiaoxiaozi|2009-04-02|demo@xiaoxiaozi.com
If I want to get each field operation as follows

The code is as follows Copy Code
$shadu _url = "http://localhost/demo.html";
$shadu _str = file_get_contents ($shadu _url);
$line _arr = Explode ("rn", $shadu _str);
$result = Array ();
if (Empty ($line _arr))
{
return Array ();
}
foreach ($line _arr as $line _str)
{
$record _arr = explode ("|", $line _STR);
if ($record _arr[0] = = "-1")
{
Log
Continue
}
$data = Array ();
$data [' id '] = $record _arr[0];
$data [' name '] = $record _arr[1];
$data [' time '] = $record _arr[2];
$data [' email '] = $record _arr[3];
$result [] = $data;
}
Var_dump ($result);

Check it out for yourself, haha.

Example 2

Capturing the contents of an HTML page

The code is as follows Copy Code
Nationwide, judging whether the $request_uri contains HTML
if (!strpos ($_server["Request_uri"], ". html"))
{
$page = "http://qq.ip138.com/weather/";
$html = file_get_contents ($page, ' R ');
$pattern = "/National major cities, counties and the next five days of weather trend forecast online query(.*?) /si ";
The HTML between regular matches
Preg_match ($pattern, $html, $PG);
echo "";
Regular replace remote address to local address
$p =preg_replace ('//weather/(w+)/index.htm/', ' tq.php/$1.html ', $PG [1]);
Echo $p;
}
Province, the judge condition is $request_uri whether contain?
else if (!strpos ($_server["Request_uri"], "?")) {
Yoyo recommends using split to get data, here is the name of the province to get
$province =explode ("/", $_server["Request_uri"]);
$province =explode (".", $province [Count ($province)-1]);
$province = $province [0];
I write my own regular notes, I feel bad, but the effect is equal to the above
Preg_match ('/[^/]+[. ( html)]$/', $_server["Request_uri"], $pro);
$province =preg_replace ('/.html/', ', $pro [0]);
$page = "http://qq.ip138.com/weather/". $province. " /index.htm ";
Try to open the page before getting the HTML data to prevent a malicious input address from causing an error
if (! @fopen ($page, "R")) {
Die ("Sorry, this address does not exist! Click here to return");
Exit (0);
}
$html = file_get_contents ($page, ' R ');
$pattern = "/five-day weather trend forecast(.*?) Please input city/si ";
Preg_match ($pattern, $html, $PG);
echo "";
Regular replacement, get province, city
$p =preg_replace ('//weather/(w+)/(w+). htm/', ' $2.html?pro=$1 ', $PG [1]);
Echo $p;
}
else {
City, pass the province through get
$pro =$_request[' Pro '];
$city =explode ("/", $_server["Request_uri"]);
$city =explode (".", $city [Count ($city)-1]);
$city = $city [0];
Preg_match ('/[^/]+[. ( HTML)]+[?] /', $_server["Request_uri"], $cit);
$city =preg_replace ('/.html?/', ', $cit [0]);
$page = "http://qq.ip138.com/weather/". $pro. " /". $city.". HTM ";
if (! @fopen ($page, "R")) {
Die ("Sorry, this address does not exist! Click here to return");
Exit (0);
}
$html = file_get_contents ($page, ' R ');
$pattern = "/five-day weather trend forecast(.*?) Please input city/si ";
Preg_match ($pattern, $html, $PG);
echo "";
Get the real picture address
$p =preg_replace ('//image//', ' http://qq.ip138.com/image/', $PG [1]);
Echo $p;
}
?>

http://www.bkjia.com/PHPjc/629097.html www.bkjia.com true http://www.bkjia.com/PHPjc/629097.html techarticle Let's take a look at the PHP manual about the file_get_contents () function to read the entire file into a string and file (), unlike the file_get_contents () that reads the files into a string. ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.