PHP/Regex:如何替换PHP脚本中的所有字符串


PHP/Regex: How to replace all Strings in PHP Script

我有一个php脚本在文本文件。我想如何创建HTML语法高亮显示。这意味着,我必须替换所有的字符串,并在前面添加一个span标签,在最后添加结束标签。

如果我正在读取的txt文件是这样的

<?php
function test() {
    echo "this is output" . getB() . 'another string';
}
function getB() {
    retrurn "b";
}
test();
?>

在我的regex后面应该是这样的:

<?php
function test() {
    echo <span style='color: red;'>"this is output"</span> . getB() . <span style='color: red;'>'another string'</span>;
}
function getB() {
    retrurn <span style='color: red;'>"b"</span>;
}
test();
?>

如何使用正则表达式解决这个问题?谢谢你的帮助!

highlight_string将PHP代码语法高亮显示为HTML。