PHP crawls arrays on the page and cyclically outputs them online.

Source: Internet
Author: User
PHP captures the array on the page and cyclically outputs the urgent online content. I used file_get_contents () to capture the content on this website.
Http://simonfenci.sinaapp.com/index.php? Key = simon & wd = 1314abc

It seems that an array is reversed .. However, no matter how you use the foreach loop, an error is reported ..

I just want to retrieve the values in the word in the array .. Who can help me?


Reply to discussion (solution)

You get a string. So you certainly don't need foreach.

Obtain the regular value or other methods in word.

$s = file_get_contents('http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc');preg_match_all('/\[word\] => (.+)/', $s, $m);print_r($m[1]);
Array(    [0] => 1314    [1] => abc)

$s=file_get_contents('http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc');$rule='#(?<=\[word\] =>)\s\w+#';preg_match_all($rule,$s,$arr);print_r($arr);


Array(    [0] => Array        (            [0] =>  1314            [1] =>  abc        ))

File_get_contents () captures the page and returns a string.

If you need to call through a remote interface, we recommend that you http://simonfenci.sinaapp.com/index.php? The output of key = simon & wd = 1314abc can be changed to a string in json format.

Use json_decode to decode the content obtained at the call to obtain the php array.

In addition, it is not recommended to use file_get_contents to obtain the content of a remote webpage. curl is recommended.


Http://simonfenci.sinaapp.com/index.php? Key = simon & wd = 1314abc
The returned result is:
String (247) "Array ([0] => Array ([word] => 1314 [word_tag] => 90 [index] => 0) [1] => Array ([word] => abc [word_tag] => 95 [index] => 1 ))"

// A string with an array structure instead of an array

// Encoding

$arr = array(    0=>array(        'word '=> 1314,        'word_tag'=> 90,        'index' => 0    ),    1 => Array(        'word' => 'abc',        'word_tag' => 95,        'index' => 1    ));echo( json_encode($arr) );


// Decoding

$arr    = array();$url    = 'http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc';$ch     = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HEADER, 0);$output = curl_exec($ch);$arr    = json_decode($output,true);curl_close($ch);


You can also use the serialize () and unserialize () serialization functions to replace json.



Supplement:

// Json returned string
[{"Word": 1314, "word_tag": 90, "index": 0 },{ "word": "abc", "word_tag": 95, "index ": 1}]

// String returned by serialize
A: 2: {I: 0; a: 3: {s: 5: "word"; I: 1314; s: 8: "word_tag"; I: 90; s: 5: "index"; I: 0;} I: 1; a: 3: {s: 4: "word"; s: 3: "abc"; s: 8: "word_tag"; I: 95; s: 5: "index"; I: 1 ;}}

Obviously, the output is shorter than the direct var_export ($ val, true); and can be easily restored.

Thank you for your consideration.

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.