本帖最后由 wudi6850346 于 2011-11-10 12:09:10 编辑

解决方案 »

  1.   

    写完后刷了下,就发现有人回答了我用到的函数。
    下面是我的代码:
    //Powered by www.hejunbin.com$html_body = '<div class="list_enter_ipad_scroll">
    <ul class="list_enter_ipad" id="footerScroller" style="width:1370px">              <li><a href="http://ipad.qq.com/" class="tencentIpad">首页</a></li>              
    <li><a href="http://ipad.qq.com/ent/index.htm" class="entIpad">文娱</a></li>              
    <li><a href="http://ipad.qq.com/pic/index.htm" class="picIpad">图片</a></li>              
    <li><a href="http://ipad.qq.com/movie/index.htm" class="movieIpad">电影</a></li>              
    <li><a href="http://ipad.qq.com/tv/index.htm" class="tvIpad">电视剧</a></li>              
    <li><a href="http://ipad.qq.com/variety/index.htm" class="varietyIpad">综艺</a></li>     </ul></div>';
    $hejunbin = array();
    $html_body = preg_replace('#(href=")(.*?)(")#e',
                  "stripslashes('\\1').hejunbin('\\2').stripslashes('\\3')",
                  $html_body);
    function hejunbin($value) {
    global $hejunbin;
    $key = microtime(true).mt_rand(10, 99).'.html';
    $hejunbin[$key] = $value;

    return $key;
    }echo '<pre>';
    echo $html_body;
    print_r($hejunbin);
        首页
        文娱
        图片
        电影
        电视剧
        综艺Array
    (
        [1320900556.3138.html] => http://ipad.qq.com/
        [1320900556.3177.html] => http://ipad.qq.com/ent/index.htm
        [1320900556.3148.html] => http://ipad.qq.com/pic/index.htm
        [1320900556.3188.html] => http://ipad.qq.com/movie/index.htm
        [1320900556.3184.html] => http://ipad.qq.com/tv/index.htm
        [1320900556.3196.html] => http://ipad.qq.com/variety/index.htm
    )
      

  2.   


    $html = <<<HTML
    <div class="list_enter_ipad_scroll">
    <ul class="list_enter_ipad" id="footerScroller" style="width:1370px">              <li><a href="http://ipad.qq.com/" class="tencentIpad">首页</a></li>              
    <li><a href="http://ipad.qq.com/ent/index.htm" class="entIpad">文娱</a></li>              
    <li><a href="http://ipad.qq.com/pic/index.htm" class="picIpad">图片</a></li>              
    <li><a href="http://ipad.qq.com/movie/index.htm" class="movieIpad">电影</a></li>              
    <li><a href="http://ipad.qq.com/tv/index.htm" class="tvIpad">电视剧</a></li>              
    <li><a href="http://ipad.qq.com/variety/index.htm" class="varietyIpad">综艺</a></li>     </ul></div>
    HTML;$arr = array();function getmicrotime(){
    return str_replace('.', '', microtime(1)).mt_rand(0, 9).mt_rand(0, 9);
    }
    function foo($m){
    global $arr;
    static $i=0;
    $main_domain = 'http://www.xxx.com/';
    $timestamp = time();
    $ret = $main_domain.getmicrotime().'.html';
    $arr[$i]['original'] = $m[1];
    $arr[$i++]['replace'] = $ret; 
    return $ret;
    }
    $pattern = '/(?=http)(.*?)(?=")/';
    $res = preg_replace_callback($pattern, 'foo', $html);echo '<pre>';
    print_r($arr);
    echo '</pre>';echo $res;
    /**
    Array
    (
        [0] => Array
            (
                [original] => http://ipad.qq.com/
                [replace] => http://www.xxx.com/1320901375312554.html
            )    [1] => Array
            (
                [original] => http://ipad.qq.com/ent/index.htm
                [replace] => http://www.xxx.com/1320901375312508.html
            )    [2] => Array
            (
                [original] => http://ipad.qq.com/pic/index.htm
                [replace] => http://www.xxx.com/1320901375312637.html
            )    [3] => Array
            (
                [original] => http://ipad.qq.com/movie/index.htm
                [replace] => http://www.xxx.com/1320901375312633.html
            )    [4] => Array
            (
                [original] => http://ipad.qq.com/tv/index.htm
                [replace] => http://www.xxx.com/1320901375312659.html
            )    [5] => Array
            (
                [original] => http://ipad.qq.com/variety/index.htm
                [replace] => http://www.xxx.com/1320901375312651.html
            ))
    */
      

  3.   

    在此感谢  hejunbin 和 ZT_King 的给力答案!谢谢你们。我采用了 hejunbin 的code ,网站上线将会有说明:【部分代码贡献人:hejunbin】同样也感谢ZT_King,你们辛苦了,喝杯coffee
      

  4.   

    preg_replace
    preg_replace_callback