在双引号内将字符串添加到字符串

时间:2013-03-23 12:36:24

标签: php regex

我设法让这个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但无法使其工作,我需要它。

由于

3 个答案:

答案 0 :(得分:0)

怎么样:

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

答案 1 :(得分:0)

你走了。

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

我认为您正在寻找的缺失部分是反向引用,\ 1。它表示模式中第一个paranthesis中的任何内容。如果你有一个以上的paranthesis,\ 2,\ 3等代表那些。

答案 2 :(得分:0)

查看preg_match,返回与正则表达式的匹配并放置它或使用连接。

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

你可以使用preg_replace但是你的意图似乎更复杂。

相关问题