我正在尝试在 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 相同的帖子请求。
感谢所有帮助,
欢呼声。