2013-03-23 129 views
0

我设法让这个正则表达式的双引号内匹配任何东西:添加HTML字符串双引号内

"([^"]|\\")*" 

它匹配

至于我,我什么都不知道有任何把握,但星星的景象让我梦想着。“ - 文森特梵高

我需要做什么,使用PHP是适用的HTML比赛,所以上面的字符串将成为...

<i class="quote">"For my part I know nothing with any certainty, but the sight of the stars makes me dream."</i> - Vincent Van Gogh 

我已经试过了preg_replace,但不能得到它工作如何我需要它。

感谢

+0

将你的正则表达式改为'(“[^”] ++“)' – lazyhammer 2013-03-23 12:38:44

+0

或者'(?<!\\)”。*?(?<!\\)“'如果你需要逃避支持 – lazyhammer 2013-03-23 12:42:43

回答

0

如何:

$str = preg_replace('/("([^"]|\\")*")/', "<i class='quote'>$1</i>", $str); 
+0

哦我的,像汤普森和里奇:) – cmc 2013-03-23 12:45:52

0

在这里你去。

echo preg_replace('#"([^"]++)*"#', '<i class="quote">"\1"</i>', 'foo bar "fuz" "buz"') 

我觉得你正在寻找的缺失部分是反向引用,\ 1。它代表你的模式中的第一个paint中的任何东西。如果你有多个paranthesis,\ 2,\ 3等代表那些。