Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 怎么实现只输出“name” “location” “address”到本地txt中

怎么实现只输出“name” “location” “address”到本地txt中

Jun 23, 2016 pm 01:55 PM
address location txt output

$url = 'http://api.map.baidu.com/place/v2/search?&query=%E9%93%B6%E8%A1%8C&location=39.915,116.404&radius=2000&output=json&ak=kvfo779F6gthGiGq4DFnOGHn';
$html = file_get_contents($url);
// $txt=file($html);
echo $html;
file_put_contents('a1.txt',$html);
?>
我现在只能输出全部的信息到本地txt中!就是不明白怎么判断输出那3个信息到本地txt中!求指导


回复讨论(解决方案)

1

$url = 'http://api.map.baidu.com/place/v2/search?&query=%E9%93%B6%E8%A1%8C&location=39.915,116.404&radius=2000&output=json&ak=kvfo779F6gthGiGq4DFnOGHn';$html = file_get_contents($url);$json = json_decode($html, 1);$d = array('name' => '', 'location' => '', 'address' => '');foreach($json['results'] as $t) {  $res[] = array_intersect_key($t, $d);}file_put_contents('a1.txt', json_encode($res));

Copy after login
Copy after login
Copy after login

1

$url = 'http://api.map.baidu.com/place/v2/search?&query=%E9%93%B6%E8%A1%8C&location=39.915,116.404&radius=2000&output=json&ak=kvfo779F6gthGiGq4DFnOGHn';$html = file_get_contents($url);$json = json_decode($html, 1);$d = array('name' => '', 'location' => '', 'address' => '');foreach($json['results'] as $t) {  $res[] = array_intersect_key($t, $d);}file_put_contents('a1.txt', json_encode($res));

Copy after login
Copy after login
Copy after login

+1

大神,非常感谢你!但是为什么我输出的是一堆乱码饿了?[{"name":"\u4e2d\u56fd\u5de5\u5546\u94f6\u884c(\u548c\u5e73\u95e8\u5185\u652f\u884c)","location":{"lat":39.90742,"lng":116.390732},"address":"\u5317\u4eac\u5e02\u897f\u57ce\u533a\u5317\u65b0\u534e\u8857\u4e1c\u677e\u6811\u80e1\u540c\u753231\u53f7"},{"name":"\u4e2d\u56fd\u5de5\u5546\u94f6\u884c(\u4e1c\u4ea4\u6c11\u5df7\u50a8\u84c4\u6240)","location"。。。。。

1

$url = 'http://api.map.baidu.com/place/v2/search?&query=%E9%93%B6%E8%A1%8C&location=39.915,116.404&radius=2000&output=json&ak=kvfo779F6gthGiGq4DFnOGHn';$html = file_get_contents($url);$json = json_decode($html, 1);$d = array('name' => '', 'location' => '', 'address' => '');foreach($json['results'] as $t) {  $res[] = array_intersect_key($t, $d);}file_put_contents('a1.txt', json_encode($res));

Copy after login
Copy after login
Copy after login



大神,非常感谢你!但是为什么我输出的是一堆乱码饿了?[{"name":"\u4e2d\u56fd\u5de5\u5546\u94f6\u884c(\u548c\u5e73\u95e8\u5185\u652f\u884c)","location":{"lat":39.90742,"lng":116.390732},"address":"\u5317\u4eac\u5e02\u897f\u57ce\u533a\u5317\u65b0\u534e\u8857\u4e1c\u677e\u6811\u80e1\u540c\u753231\u53f7"},{"name":"\u4e2d\u56fd\u5de5\u5546\u94f6\u884c(\u4e1c\u4ea4\u6c11\u5df7\u50a8\u84c4\u6240)","location"。。。。。

处理一下

1

function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');echo urldecode(json_encode($res));

Copy after login
Copy after login

处理一下

1

function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');echo urldecode(json_encode($res));

Copy after login
Copy after login



现在在网页的页面能正确显示了!但是在txt中还是乱码!
[{"name":"%E4%B8%AD%E5%9B%BD%E5%B7%A5%E5%95%86%E9%93%B6%E8%A1%8C%28%E5%92%8C%E5%B9%B3%E9%97%A8%E5%86%85%E6%94%AF%E8%A1%8C%29","location":{"lat":"39.90742","lng":"116.390732"},"address":"%E5%8C%97%E4%BA%AC%E5%B8%82%E8%A5%BF%E5%9F%8E%E5%8C%BA%E5%8C%97%E6%96%B0%E5%8D%8E%E8%A1%97%E4%B8%9C%E6%9D%BE%E6%A0%91%E8%83%A1%E5%90%8C%E7%94%B231%E5%8F%B7"},{"name":"%E4%B8%AD%E5%9B%BD%E5%B7%A5%E5%95%86%E9%93%B6%E8%A1%8C%28%E4%B8%9C%E4%BA%A4%E6%B0%91%E5%B7%B7%E5%82%A8%E8%93%84%E6%89%80%29","location":{"lat":"39.908091","lng":"116.413558"},"

你保存的不是 echo 的结果吗?

1

function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');$t = urldecode(json_encode($res));file_put_contents('a1.txt', $t);

Copy after login


说实在的,不会变通的人,是不适合做程序员的

你保存的不是 echo 的结果吗?

1

function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');$t = urldecode(json_encode($res));file_put_contents('a1.txt', json_encode($t));

Copy after login
Copy after login
Copy after login


说实在的,不会变通的人,是不适合做程序员的


你说过的方式我试过的了!网上其他一些避免乱码的也试过的!还是乱码了!我也刚接触PHP,所以很不多不理解!做的不好的请多见谅!


你保存的不是 echo 的结果吗?

1

function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');$t = urldecode(json_encode($res));file_put_contents('a1.txt', json_encode($t));

Copy after login
Copy after login
Copy after login


说实在的,不会变通的人,是不适合做程序员的


你说过的方式我试过的了!网上其他一些避免乱码的也试过的!还是乱码了!我也刚接触PHP,所以很不多不理解!做的不好的请多见谅!



这样就可以了,上面那个多了一次json_encode。

1

<!--?php$url = 'http://api.map.baidu.com/place/v2/search?&query=%E9%93%B6%E8%A1%8C&location=39.915,116.404&radius=2000&output=json&ak=kvfo779F6gthGiGq4DFnOGHn';$html = file_get_contents($url); $json = json_decode($html, 1); $d = array('name' =--> '', 'location' => '', 'address' => '');foreach($json['results'] as $t) {  $res[] = array_intersect_key($t, $d);}function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');$t = urldecode(json_encode($res));file_put_contents('a1.txt', $t);?>

Copy after login
Copy after login



你保存的不是 echo 的结果吗?

1

function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');$t = urldecode(json_encode($res));file_put_contents('a1.txt', json_encode($t));

Copy after login
Copy after login
Copy after login


说实在的,不会变通的人,是不适合做程序员的


你说过的方式我试过的了!网上其他一些避免乱码的也试过的!还是乱码了!我也刚接触PHP,所以很不多不理解!做的不好的请多见谅!



这样就可以了,上面那个多了一次json_encode。

1

<!--?php$url = 'http://api.map.baidu.com/place/v2/search?&query=%E9%93%B6%E8%A1%8C&location=39.915,116.404&radius=2000&output=json&ak=kvfo779F6gthGiGq4DFnOGHn';$html = file_get_contents($url); $json = json_decode($html, 1); $d = array('name' =--> '', 'location' => '', 'address' => '');foreach($json['results'] as $t) {  $res[] = array_intersect_key($t, $d);}function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');$t = urldecode(json_encode($res));file_put_contents('a1.txt', $t);?>

Copy after login
Copy after login



谢谢了啊!代码应该没问题了,我觉得我的文件设置的编码有问题!不过还是非常感谢你!
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is Intel TXT? What is Intel TXT? Jun 11, 2023 pm 06:57 PM

IntelTXT is a hardware-assisted security technology launched by Intel. It can ensure the integrity and security of the server during startup by establishing a protected space between the CPU and BIOS. The full name of TXT is TrustedExecutionTechnology, which is Trusted Execution Technology. Simply put, TXT is a security technology that provides hardware-level protection to ensure that the server has not been modified by malicious programs or unauthorized software when it is started. this one

How to convert html to txt How to convert html to txt Aug 31, 2023 am 09:23 AM

Methods for converting html to txt include using a text editor, using online conversion tools, and using Python programming. Detailed introduction: 1. To open an HTML file, you can use any text editor, such as Notepad, Sublime Text, etc. To select the content of the entire HTML file, you can press the Ctrl+A shortcut key or drag the mouse to select and copy the selection. The content can be copied by pressing the Ctrl+C shortcut or through the copy option in the right-click menu, opening a new TXT file, using the same text editor, etc.

How to convert chm to txt How to convert chm to txt Oct 17, 2023 pm 02:42 PM

chm is converted to txt by using online conversion tools, using browser plug-ins, using command line tools and using third-party software. Detailed introduction: 1. Use the online conversion tool, just upload the CHM file, select the TXT format, and then download the converted TXT file; 2. Use the browser plug-in, after installing the plug-in, just open the CHM file in the browser, and then Click the plug-in button to convert CHM files into TXT format; 3. Use command line tools, etc.

Analysis of location configuration examples in Nginx server Analysis of location configuration examples in Nginx server May 24, 2023 pm 02:05 PM

First, let me briefly introduce the types of location and matching rules, using the example of nginxwiki as an example: location=/{#matchesthequery/only.[configurationa]}location/{#matchesanyquery,sinceallqueriesbeginwith/,butregular#expressionsandanylongerconventionalblockswillbe#matchedfirst.[ configurationb]}location^~/im

FAQ for pandas reading txt files FAQ for pandas reading txt files Jan 19, 2024 am 09:19 AM

Pandas is a data analysis tool for Python, especially suitable for cleaning, processing and analyzing data. During the data analysis process, we often need to read data files in various formats, such as Txt files. However, some problems will be encountered during the specific operation. This article will introduce answers to common questions about reading txt files with pandas and provide corresponding code examples. Question 1: How to read txt file? txt files can be read using the read_csv() function of pandas. This is because

How to intercept uri in nginx location How to intercept uri in nginx location May 18, 2023 pm 12:07 PM

Note: The root and aliasroot instructions in location only set the search root to the directory set by root, that is, the uri will not be truncated. Instead, the original uri will be used to jump to the directory to find the file. The aias instruction will truncate the matching uri, and then Use the path set by alias plus the remaining uri as a sub-path to find the uri of proxy_pass in location. If the url of proxy_pass does not have uri, if the tail is "/", the matching uri will be truncated. If the tail is not "/", then Will not truncate the matching uri if the proxy_pass url contains uri

How to output text with line breaks in Go language How to output text with line breaks in Go language Mar 15, 2024 pm 04:15 PM

Go language is a modern, efficient and concise programming language that is widely used in software development in various fields. In the Go language, outputting text with newlines is very simple and can be achieved by using the Println function provided by the fmt package. Below we will introduce in detail how to output text with line breaks in Go language, as well as related code examples. In the Go language, if you want to output text with newlines, you can use the Println function provided by the fmt package. The Println function will output text in

How to configure location and rewrite rules in Nginx How to configure location and rewrite rules in Nginx May 18, 2023 pm 12:25 PM

Location tutorial example: location=/{#Exact match/, the host name cannot be followed by any string [configurationA]}location/{#Because all addresses begin with /, this rule will match all requests#But regular and the longest string will be matched first [configurationB]}location/documents/{#Match any address starting with /documents/. After matching, continue to search downwards#Only when the subsequent regular expression is not matched, This article will use [configurationC]}location~/document

See all articles