添加HTML字符串双引号内
问题描述:
我设法让这个正则表达式的双引号内匹配任何东西:添加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,但不能得到它工作如何我需要它。
感谢
答
如何:
$str = preg_replace('/("([^"]|\\")*")/', "<i class='quote'>$1</i>", $str);
+0
哦我的,像汤普森和里奇:) – cmc 2013-03-23 12:45:52
答
在这里你去。
echo preg_replace('#"([^"]++)*"#', '<i class="quote">"\1"</i>', 'foo bar "fuz" "buz"')
我觉得你正在寻找的缺失部分是反向引用,\ 1。它代表你的模式中的第一个paint中的任何东西。如果你有多个paranthesis,\ 2,\ 3等代表那些。
答
看看preg_match,用你的正则表达式返回一个匹配并放置它或者使用一个连接。
http://www.php.net/manual/en/function.preg-match.php
你可以使用的preg_replace但它似乎是你的意图更令人费解。
将你的正则表达式改为'(“[^”] ++“)' – lazyhammer 2013-03-23 12:38:44
或者'(? lazyhammer 2013-03-23 12:42:43