PHP crawls the array on the page and loops out the emergency line etc.

Source: Internet
Author: User
I used file_get_contents () to crawl the content on this web site.
Http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc

It appears as if the inverse is an array: But I don't know how to use the Foreach loop to give an error.

I just want to take the value out of the word in the array. Somebody help me, hurry.


Reply to discussion (solution)

What you get here is a string. So definitely don't use foreach.

Get a regular or other method of face value in Word.

$s = file_get_contents (' http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc ');p reg_match_all ('/\[ Word\] = (. +)/', $s, $m);p rint_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+# ';p reg_match_all ($rule, $s, $arr);p Rint_r ($arr);


Array (    [0] = = Array        (            [0] = =  1314            [1] = =  ABC        ))

The file_get_contents () Fetch page returns a string.

If you need to call through a remote interface, it is recommended that the output of http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc be changed to a JSON-formatted string.

The PHP array can be obtained by decoding with Json_decode when the content is fetched at the call.

Also, it is not recommended to use file_get_contents to get the contents of a remote Web page, curl is recommended.


Http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc
The returned is:
String (247) "Array ([0] = = Array ([Word] = 1314 [Word_tag] = [index] = 0) [1] = = Array ([Word] =& Gt ABC [Word_tag] = [index] = 1) "

A string of an array structure, not an array

Coding

$arr = Array (    0=>array (        ' word ' = = 1314, ' word_tag ' = ' = ',        ' index ' = 0    ),    1 = Array (        ' word ' = ' abc ',        ' word_tag ' = ' + ',        ' 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 replace the JSON with serialize () and Unserialize (), the serialization function.



Add:

String returned by JSON
[{"word": 1314, "Word_tag": +, "index": 0},{"word": "abc", "Word_tag": Up, "index": 1}]

Serialize the returned string
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;}}

Significantly more than direct Var_export ($val, true); The output is shorter and can be easily restored.

Thank you, and finally it's done.

  • 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.