替换报价和内部的所有内容,直到有一个未转义的报价可用

时间:2017-01-27 12:35:00

标签: php

我正在尝试在 PHP Ajax 中开发一个实时文本格式化程序,到目前为止一切顺利。但是,我遇到了一个我似乎无法弄清楚的问题。

基本上,我想要发生的是,让我们说我有这三个字符串:

"This is some text \" (and a double quote)"
'This is some text \' (and a single quote)'
"This is some more text"

我希望它成为:

<span id="ff3814">"This is some text \" (and a double quote)"</span>
<span id="ff3814">'This is some text \' (and a single quote)'</span>
<span id="ff3814">"This is some more text"</span>

请注意span 未转义报价(与开头报价相同的类型)之前不会关闭的方式

我尝试使用正则表达式

做一些事情
$output = 'Other content... "This is some text \" (and a double quote)"'. 

$output = preg_replace('/"[^"]+"/', "<span id='ff3814'>$1</span>", $output);
// should output: Other content... <span id='ff3814'>"This is some text \" (and a double quote)"</span>

但是,它只会输出span标记,而不会输出任何实际内容。不仅如此,我很快就意识到它无论如何都不适用于未转义的引号

我如何获得此功能?我在 Google 的第一页或第二页上找不到任何与之相关的内容,而且似乎没有任何 StackOverflow 相同的帖子请求。

感谢所有帮助,
欢呼声。

0 个答案:

没有答案