欢迎来到代码驿站!

PHP代码

当前位置:首页 > 软件编程 > PHP代码

php file_get_contents取文件中数组元素的方法

时间:2021-07-19 08:01:05|栏目:PHP代码|点击:

用file_get_contents()抓取了 这个网址上的内容

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

看似好像反回的是数组。。但是我不管怎么用foreach循环都报错。。

我只想把数组中的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函数返回数组中所有值的总和。
echo '文件中所有jb之和为:'.array_sum($arr_num);
?>

array.txt内容如下所示:

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

上一篇:laravel config文件配置全局变量的例子

栏    目:PHP代码

下一篇:php基础知识:类与对象(1)

本文标题:php file_get_contents取文件中数组元素的方法

本文地址:http://www.codeinn.net/misctech/159211.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有