1

我有这个代码来替换每个数字(和点)并将其替换为<b>and the text</b>

<?

function reem2($cadena) {

   $buscarRegex = array('/^([0-9]{1}|[.])$/i');
   $reemplazo = array('<b>$i</b>');
   $mag = preg_replace($buscarRegex, $reemplazo, $cadena);

   return $cadena;
}

$string = "1. Krewella - Can't Control Myself
2. Kdrew - Circles
3. Korn Feat. Skrillex & Kill The Noise - Narcissistic Cannibal
4. Netsky - Love Has Gone
5. Example - Midnight Run (Flux Pavilion Remix)
6. Madeon - Finale (Radio Version)
7. Feed Me Vs. Knife Party Vs. Skrillex - My Pink Reptile Party (Maluu's Slice'n'diced Mashup)
8. Krewella & Pegboard Nerds - This Is Not The End
9. Skrillex - Bangarang
10. The Prototypes - Suffocate
11. Ayah Marar - Mind Controller (Cutline Remix)
12. Skrillex Feat. Krewella - Breathe (Vocal Edit)
13. Utah Saints Vs. Drumsound & Bassline Smith - What Can You Do For Me (Tantrum Desire Remix)
14. Nero - Promises (Skrillex & Nero Remix)
15. 20 Florence & The Machine - Cosmic Love (Seven Lions Remix)";

echo reem2(nl2br($string));

?>

但它不起作用,它不会改变任何东西:

HTML 中的输出将是:

1. Krewella - 无法控制自己

2. Kdrew - 圈子

3. Korn 壮举。Skrillex & Kill The Noise - 自恋的食人族

4. Netsky - 爱已逝去

5.示例 - Midnight Run (Flux Pavilion Remix)

6. Madeon - Finale (电台版)

7.喂我VS。刀党VS。Skrillex - 我的粉红爬行动物派对(Maluu's Slice'n'diced Mashup)

8. Krewella & Pegboard Nerds - 这不是结束

9. Skrillex - 班加朗

10.原型-窒息

11. Ayah Marar - Mind Controller (Cutline Remix)

12. Skrillex 壮举。Krewella - 呼吸(人声编辑)

13.犹他州圣徒队 Drumsound & Bassline Smith - What Can You Do For Me (Tantrum Desire Remix)

14. Nero - Promises (Skrillex & Nero Remix)

15. 20 Florence & The Machine - Cosmic Love (七狮混音)

我能做些什么?

4

5 回答 5

2

您的正则表达式已损坏:

/^([0-9]{1}|[.])$/i
 ^-- start of line 
                ^--- end of line

您只允许在一行上单独使用一个 SINGLE 字符,因此正则表达式永远无法匹配任何内容。

你可能想要更像这样的东西:

/^([\d]+)\./

这将匹配行首的任意数量的数字,后跟单个..

于 2013-07-08T19:45:21.710 回答
1
function reem2($cadena) {

   $buscarRegex = array('/^([0-9]+\.)/m'); // changed modifier to multiline
   $reemplazo = array('<b>$1</b>'); // changed replacement to a capture offset
   return preg_replace($buscarRegex, $reemplazo, $cadena);
}
于 2013-07-08T19:47:45.807 回答
1

因此,从我所见,您的正则表达式不正确,并且您在 function 中返回了不正确的变量reem2,因此请尝试用类似这样的内容替换您的函数

function reem2($cadena) {
    return preg_replace("/([0-9]+\.)/", "<b>$1</b>", $cadena);
}
于 2013-07-08T19:48:30.067 回答
1
return $cadena;

是您的问题,您进行替换然后丢弃结果并返回输入

return $mag;

可能是你的意思

事实上你的正则表达式也是错误的

function reem2($cadena) {

   $buscarRegex = array('/^([0-9]{1,2}\.)(.*)$/m');
   $reemplazo = array('<b>\1</b>\2');
   $mag = preg_replace($buscarRegex, $reemplazo, $cadena);

   return $mag;
}

似乎是你想要的。

于 2013-07-08T19:43:34.067 回答
1

您可以使用以下代码:

function reem2($cadena) {
   $buscarRegex = array('/^(\d+\.)/mi'); // This means match any digit(s) followed by a dot at the beginning of each line. Note the m modifier
   $reemplazo = array('<b>$1</b>'); // replace should be with group 1, not some vague $i
   $mag = preg_replace($buscarRegex, $reemplazo, $cadena);
   return $mag; // return value: fixed
}
于 2013-07-08T19:48:33.760 回答