求个PHP代码,对file_get_contents结果中的连接地址替换为序号
求个PHP代码,对file_get_contents结果中的连接地址替换为序号。file_get_contents结果如下(htm文件名是数字,已经是从小到大排序的):
<div><a href="/a/20160207/149505.htm">时尚历史EDD</a></div>
<div><a href="/a/20160207/149754.htm">时尚历史ERR</a></div>
<div><a href="/a/20160207/149901.htm">时尚历史QQS</a></div>
<div><a href="/a/20160207/150006.htm">时尚历史FCC</a></div>
<div><a href="/a/20160207/151230.htm">时尚历史DEZ</a></div>
<div><a href="/a/20160207/153100.htm">时尚历史DHV</a></div>
<div><a href="/a/20160207/155213.htm">时尚历史DWA</a></div>
<div><a href="/a/20160207/157144.htm">时尚历史DCZ </a></div>
<div><a href="/a/20160207/159622.htm">时尚历史DRR</a></div>
实现为(htm文件名替换为序号1.2.3……):
<div><a href="/a/20160207/1.htm">时尚历史EDD</a></div>
<div><a href="/a/20160207/2.htm">时尚历史ERR</a></div>
<div><a href="/a/20160207/3.htm">时尚历史QQS</a></div>
<div><a href="/a/20160207/4.htm">时尚历史FCC</a></div>
<div><a href="/a/20160207/5.htm">时尚历史DEZ</a></div>
<div><a href="/a/20160207/6.htm">时尚历史DHV</a></div>
<div><a href="/a/20160207/7.htm">时尚历史DWA</a></div>
<div><a href="/a/20160207/8.htm">时尚历史DCZ </a></div>
<div><a href="/a/20160207/9.htm">时尚历史DRR</a></div>
求高手给个php代码,感谢。
少许积分,不成敬意。 $myHtml = '<div><a href="/a/20160207/149505.htm">时尚历史EDD</a></div>
<div><a href="/a/20160207/149754.htm">时尚历史ERR</a></div>
<div><a href="/a/20160207/149901.htm">时尚历史QQS</a></div>
<div><a href="/a/20160207/150006.htm">时尚历史FCC</a></div>
<div><a href="/a/20160207/151230.htm">时尚历史DEZ</a></div>
<div><a href="/a/20160207/153100.htm">时尚历史DHV</a></div>
<div><a href="/a/20160207/155213.htm">时尚历史DWA</a></div>
<div><a href="/a/20160207/157144.htm">时尚历史DCZ </a></div>
<div><a href="/a/20160207/159622.htm">时尚历史DRR</a></div>';
$out = preg_replace_callback(
"/<a href=\"\/a\/20160207\/(.*)\.htm\">/",
function($m) {
static $id = 0;
$id++;
return str_replace($m, $id, $m);
},
$myHtml);
var_dump($out);
结果:
string '<div><a href="/a/20160207/1.htm">时尚历史EDD</a></div>
<div><a href="/a/20160207/2.htm">时尚历史ERR</a></div>
<div><a href="/a/20160207/3.htm">时尚历史QQS</a></div>
<div><a href="/a/20160207/4.htm">时尚历史FCC</a></div>
<div><a href="/a/20160207/5.htm">时尚历史DEZ</a></div>
<div><a href="/a/20160207/6.htm">时尚历史DHV</a></div>
<div><a href="/a/20160207/7.htm">时尚历史DWA</a></div>
<div><a href="/a/20160207/8.htm">时尚历史DCZ </a></div>
<div><a href="/a/20160207/9.htm'... (length=539)
BTW:
1、用了闭包我记得是php 5.3以上的版本才支持的,所以注意环境
2、赶紧放弃file_get_contents投入CURL的怀抱吧,性能差距太大了 太高深了 你应该去直呼 V2这些地方!
页:
[1]