服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - PHP教程 - php file_get_contents取文件中数组元素的方法

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

2021-05-07 16:25PHP教程网 PHP教程

下面小编就为大家带来一篇php file_get_contents取文件中数组元素的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

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

我只想把数组中的word里面的值 取出来。

方法如下:正解(其他的字段一样,把word替换即可)

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

 

我的遇到的问题如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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内容如下所示:

?
1
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取文件中数组元素的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐