Home > Backend Development > PHP Tutorial > 新手请问个PHP查找TXT内容并输出的有关问题

新手请问个PHP查找TXT内容并输出的有关问题

WBOY
Release: 2016-06-13 13:41:41
Original
930 people have browsed it

新手请教个PHP查找TXT内容并输出的问题!
新手请教个PHP查找TXT内容并输出的问题!

1.TXT里的内容如下:

HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->101010100=北京
101010200=海淀
101010300=朝阳
101010400=顺义
101010500=怀柔
101010600=通州
101010700=昌平
101010800=延庆
101010900=丰台
101011000=石景山
101011100=大兴
101011200=房山
101011300=密云
101011400=门头沟
101011500=平谷
101011600=八达岭
Copy after login


我想通过PHP来实现查询
比如http://127.0.0.1/1.php?city=北京 就能找到对应的101010100
不会PHP 只好来求助,麻烦大虾给段详细的代码!

------解决方案--------------------

//随便写了下,注意data.txt为gb2312编码
PHP code


$city=$_REQUEST["city"];
$city=iconv("utf-8","gb2312",$city);
if ($city)
{
    $fp=fopen("data.txt","r");
    while (!feof($fp))
    {
        $buffer = fgets($fp);
        list($code,$area)=explode("=",$buffer);


        if (trim($area)==$city)
        {
            echo $code;
        }

    }
    fclose($fp);

}
?>
<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨

引用:
echo urldecode("%E5%8C%97%E4%BA%AC");
输出 :北京

记得txt文件与你的php文件编码要统一,否则会乱码,或者要转化一下也可。


谢谢解答,我现在碰到的问题是
我直接用2楼的那个代码,没有任何输出,原因是查询时 汉字没有urlencode

我不知道怎么加,PHP完全不懂!
我……
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template