<?php
if(!file_exists("1.txt")){ 
    fopen("1.txt","w"); 
}
$txt = file_get_contents("http://www.baidu.com");//preg_match_all("|<a\s+href=(?<url>.+?)>(?<content>.+?)</a>|",$txt,$match);
var_dump($match);foreach ($match[0] as $val){
    echo htmlspecialchars($val)."<br>";
    $str .= $val;
}
file_put_contents("1.txt",$str);
?>
对于这段代码,如何使输出的txt文件里只显示URL和链接名?并且实现分行?比如在txt里显示的结果为
http://passport.baidu.com/?login&tpl=mn登录
http://news.baidu.com新&nbsp;闻
http://tieba.baidu.com贴&nbsp;吧
http://zhidao.baidu.com"知&nbsp;道
http://mp3.baidu.comMP3
.
.
.
.

解决方案 »

  1.   

    写到文本不是用 '<br/>' ,这是html里的换行文本换行 "\r\n" 注意是双引号至于你的 正则,有点看不明白
      

  2.   


    <?php
    if(!file_exists("1.txt")){  
      fopen("1.txt","w");  
    }
    $txt = file_get_contents("http://www.baidu.com");//
    preg_match_all("|<a\s+href=(.+?)>(.+?)</a>|",$txt,$match);
    var_dump($match);
    foreach ($match[1] as $key =>$val){
        $match[2][$key] = iconv("GB2312", "UTF-8", $match[2][$key]);
        $str .= $match[1][$key].$match[2][$key]."\n";
    }
    file_put_contents("1.txt",$str);
    ?>