将HTML添加到双引号内的字符串中


Adding HTML to string within double quotation marks

我已经设法得到这个RegExp匹配双引号内的任何内容:

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

匹配

"对我来说,我什么都不知道,但看到星星让我梦想。"——文森特·梵高

我需要做的,使用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);

给你

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

我认为你正在寻找的缺失部分是背景参考,'1。它表示模式中第一个括号中的内容。如果你有多个假数,'2、'3等表示它们。

看一下preg_match,用你的regexp返回一个匹配,并放置它或使用一个连接。

http://www.php.net/manual/en/function.preg-match.php

您可以使用preg_replace,但它似乎对您的意图更复杂。