Home >Backend Development >PHP Tutorial >php file_get_contents implements the method of getting array elements in the file

php file_get_contents implements the method of getting array elements in the file

墨辰丷
墨辰丷Original
2018-05-24 11:24:592131browse

This article mainly introduces the method of php file_get_contents to get the array elements in the file. Interested friends can refer to it. I hope it will be helpful to everyone.

Use file_get_contents() to grab the content on this URL

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

It seems like it returns an array. . But no matter how I use the foreach loop, I get an error. .

I just want to take out the value of the word in the array.

The method is as follows: Correct solution (the other fields are the same, just replace word)


$s=file_get_contents(&#39;http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc&#39;);$rule=&#39;#(?<=\[word\] =>)\s\w+#&#39;;preg_match_all($rule,$s,$arr);print_r($arr);



The problems I encountered are as follows:


<?php 
@header(&#39;content-type:text/html;charset=utf-8&#39;);
function show_bug($msg){
echo "<pre class="brush:php;toolbar:false">";
print_r($msg);
echo "
"; } //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 content is as follows:


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


The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP 7 ways to use header()

php two-dimensional array sorting

phpcms search function implementation

The above is the detailed content of php file_get_contents implements the method of getting array elements in the file. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn