求助!关于preg_match_all,preg_match本人无法解决的问题! 本帖最后由 wudi6850346 于 2011-11-10 12:09:10 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 写完后刷了下,就发现有人回答了我用到的函数。下面是我的代码://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) $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 ))*/ 在此感谢 hejunbin 和 ZT_King 的给力答案!谢谢你们。我采用了 hejunbin 的code ,网站上线将会有说明:【部分代码贡献人:hejunbin】同样也感谢ZT_King,你们辛苦了,喝杯coffee preg_replacepreg_replace_callback 急急急急!!php连接mysql问题 可不可以这样提交表单? 问一个phpmyadmin问题! 关于PHP分页显示问题!!!大家帮我看看!!!谢谢 如何读取类似http://www.abc.com/?123这样的地址栏中的参数123? UTF-8编码的程序POST数据给一个GBK编码的程序接收,应该怎么处理? 各位大虾,小弟近日被困。。。 关于文件的读写! 第一次提问,希望各位高人能给与帮助,多谢~~~~~~~~~~ PHP 一级栏目读取三级栏目下文章? 用JQuery控制网页图片宽度问题 怎样申请银行接口?
下面是我的代码:
//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
)
$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
))
*/
preg_replace_callback