我第一次在这里发帖并希望有人能够帮助我。使用带有preg_replace()的索引数组的问题PHP
我有一个文件卫生组织在610开始编号,并继续以1019我想用PHP的的preg_match()函数从0开始编号和继续,直到410
下面是一些代码,我”一直在努力。但我不能获得取代数字的功能。我不知道为什么,我没有得到任何错误。
<?php
$string = "610 611 612 613 614 615 616 617"; //this isnt the actual file but will do. The actual file is more complicated. This is just a test string.
$patterns = array();
for ($i=610; $i<1020; $i++) {
$patterns[$i] = '/$i/';
}
$replacements = array();
for ($j=1; $j<410; $j++) {
$replacements[$j] = '\r\n' . $j;
}
$newText = preg_replace($patterns, $replacements, $string);
echo $newText;
?>
我用例子#2的形式http://www.php.net/manual/en/function.preg-replace.php作为参考。
在此先感谢您的帮助:)
你能描述什么不适合你吗?输入,输出和预期输出将有助于...... – ircmaxell 2010-08-19 14:23:27
不要在''/ $ i /''和''\ r \ n''上使用单引号。改用双引号。看到这里的区别:http://php.net/manual/en/language.types.string.php – NullUserException 2010-08-19 14:24:37
我不知道,但我觉得很奇怪,在这种情况下使用正则表达式。 – BoltClock 2010-08-19 14:24:42