php file_get_contents取文件中數(shù)組元素的方法
用file_get_contents()抓取了 這個網(wǎng)址上的內(nèi)容
http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc
看似好像反回的是數(shù)組。。但是我不管怎么用foreach循環(huán)都報錯。。
我只想把數(shù)組中的word里面的值 取出來。
方法如下:正解(其他的字段一樣,把word替換即可)
$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);
我的遇到的問題如下:
<?php @header('content-type:text/html;charset=utf-8'); function show_bug($msg){ echo "<pre>"; print_r($msg); echo "</pre>"; } //ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)'); //$url="http://redhatcn.cn//Home/Login/mmzh2/t/user"; // 所有[jb] => 后面的金額,所有金額相加 $url='array.txt'; $s=file_get_contents($url); $rule='#(?<=\[jb\] =>)\s\w+#';preg_match_all($rule,$s,$arr); // $bool=is_array($arr); // var_dump($bool); //show_bug($arr); $arr_num=$arr[0]; //array_sum函數(shù)返回數(shù)組中所有值的總和。 echo '文件中所有jb之和為:'.array_sum($arr_num); ?>
array.txt內(nèi)容如下所示:
Array( [0] => Array ( [id] => 183 [cf] => 0 [date] => 2016-08-26 21:17:32 [zffs1] => 0 [zffs2] => 1 [zffs3] => 0 [jb] => 5000 [zt] => 1 [user] => 18676081117 [qr_zt] => 1 [user_tjr] => 18943636615 [user_nc] => ¿ÉÀÖ [cf_ds] => 0 [jycg_ds] => 0 [yid] => [ok] => 1 ) [1] => Array ( [id] => 240 [cf] => 0 [date] => 2016-09-03 12:06:50 [zffs1] => 1 [zffs2] => 1 [zffs3] => 1 [jb] => 8000 [zt] => 0 [user] => 15192777617 [qr_zt] => 0 [user_tjr] => 13987611435 [user_nc] => Ö콨¸Û [cf_ds] => 0 [jycg_ds] => 0 [yid] => [ok] => 1 ) )
以上這篇php file_get_contents取文件中數(shù)組元素的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
PHP數(shù)組的交集array_intersect(),array_intersect_assoc(),array_inte
求兩個數(shù)組的交集問題可以使用array_intersect(),array_inersect_assoc,array_intersect_key來實現(xiàn),其中array_intersect()函數(shù)是求兩個數(shù)的交集2011-05-05使用php+Ajax實現(xiàn)唯一校驗實現(xiàn)代碼[簡單應(yīng)用]
使用php+Ajax實現(xiàn)唯一校驗實現(xiàn)代碼[簡單應(yīng)用],前臺用ajax后臺用php,需要的朋友可以參考下。2011-11-11PHP連接MSSQL2008/2005數(shù)據(jù)庫(SQLSRV)配置實例
這篇文章主要介紹了PHP連接MSSQL2008/2005數(shù)據(jù)庫(SQLSRV)配置方法,實例講述了完整的連接與配置過程,并提供了具體的擴展文件下載與測試代碼,需要的朋友可以參考下2014-10-10