php网页抓取(foreach.得到word里面值正则或者其他的方法吧)

优采云 发布时间: 2021-11-26 00:17

  php网页抓取(foreach.得到word里面值正则或者其他的方法吧)

  你得到的是一个字符串。所以foreach是绝对不需要的。

  用word、regular或其他方法获取值。

  $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='#(?)\s\w+#';preg_match_all($rule,$s,$arr);print_r($arr);

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

  File_get_contents() 获取一个页面并返回一个字符串。

  如果需要通过远程接口调用,推荐

  输出可以改成json格式的字符串。

  调用端检索内容时使用json_decode解码,得到php数组。

  另外,不推荐使用file_get_contents获取远程网页的内容,推荐使用curl。

  返回的是:

  string(247) "Array ([0] => Array ([word] => 1314 [word_tag] => 90 [index] => 0) [1] => Array ([word] => abc [word_tag] => 95 [索引] => 1)) "

  //一个数组结构的字符串,不是数组

  //编码

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

  //解码

  $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);

  您也可以使用 serialize() 和 unserialize() 来替换 json。

  补充:

  //json 返回的字符串

  [{"word":1314,"word_tag":90,"index":0},{"word":"abc","word_tag":95,"index":1}]

  //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;}}

  明显优于直接 var_export($val,true);

  输出较短,可以轻松恢复。

  谢谢大家,终于搞定了

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线