我已经创建了一个preg_replace脚本,但现在我想在其中添加计数函数!
我的代码
$replace = 'ISO Burning Programs/Active@ ISO Burner 2.1.0.0/SPTDinst-v162-x86.exe';
$result=preg_replace('/[^0-9^A-Z^a-z-*… ,;_!@.{}#<>""=-^:()\[\]]/', '<br/>', $replace);
echo $result;输出
ISO Burning Programs
Active@ ISO Burner 2.1.0.0
SPTDinst-v162-x86.exe但我想要的输出是-
1 ISO Burning Programs
2 Active@ ISO Burner 2.1.0.0
3 SPTDinst-v162-x86.exe有人能帮我吗?
提前感谢!
发布于 2011-01-27 22:49:20
或者,您可以这样做:
$replace = 'ISO Burning Programs/Active@ ISO Burner 2.1.0.0/SPTDinst-v162-x86.exe';
$result = explode('/', $replace);
foreach($result as $i => $value)
printf("%d %s<br />", ++$i, $value);发布于 2011-01-27 23:01:06
如果要使用preg执行此操作,则必须使用preg_replace_callback
$result = preg_replace_callback('/([^\/]*)(\/|$)/', function($matches){
static $count = 0;
$count++;
return !empty($matches[1]) ? $count.' '.$matches[1].'<br/>' : '';
}, $replace);发布于 2011-01-27 22:44:56
preg_replace has a count function in it, the -1 is limit (unlimited) and $count is the number of replacement. just FYI.
$result=preg_replace('/[^0-9^A-Z^a-z-*… ,;_!@.{}#<>""=-^:()\[\]]/', '<br/>\n', $replace, -1, $count);
$a = explode("\n", $result);
$i=1;
foreach($a as $res)
{
echo $i . " " . $res;
$i++;
}https://stackoverflow.com/questions/4817594
复制相似问题