日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php页面的循环输出数组,PHP抓取页面上的数组 并循环输出 急

發布時間:2025/3/8 php 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php页面的循环输出数组,PHP抓取页面上的数组 并循环输出 急 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

PHP抓取頁面上的數組 并循環輸出 急 在線等

我用file_get_contents()抓取了 這個網址上的內容

http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc

看似好像反回的是數組。。但是我不管怎么用foreach循環都報錯。。

我只想把數組中的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

)

)

------解決思路----------------------

http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc

返回的是:

string(247) "Array ( [0] => Array ( [word] => 1314 [word_tag] => 90 [index] => 0 ) [1] => Array ( [word] => abc [word_tag] => 95 [index] => 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); 輸出的更短,并且可以輕易還原。

相關文章

相關視頻

總結

以上是生活随笔為你收集整理的php页面的循环输出数组,PHP抓取页面上的数组 并循环输出 急的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。