基本上,我需要一个正则表达式来匹配 PHP 标记内的所有双引号字符串,而其中没有变量。
这是我到目前为止所拥有的:
"([^\$\n\r]*?)"(?![\w ]*')
并替换为:
'$1'
但是,这也会匹配 PHP 标记之外的内容,例如 HTML 属性。
示例案例:
<a href="somelink" attribute="value">Here's my "dog's website"</a>
<?php
$somevar = "someval";
$somevar2 = "someval's got a quote inside";
?>
<?php
$somevar3 = "someval with a $var inside";
$somevar4 = "someval " . $var . 'with concatenated' . $variables . "inside";
$somevar5 = "this php tag doesn't close, as it's the end of the file...";
"
它应该匹配并替换应该用 a替换的所有位置'
,这意味着理想情况下应该不理会 html 属性。
替换后的示例输出:
<a href="somelink" attribute="value">Here's my "dog's website"</a>
<?php
$somevar = 'someval';
$somevar2 = 'someval\'s got a quote inside';
?>
<?php
$somevar3 = "someval with a $var inside";
$somevar4 = 'someval ' . $var . 'with concatenated' . $variables . 'inside';
$somevar5 = 'this php tag doesn\'t close, as it\'s the end of the file...';
能够匹配内部脚本标签也很棒......但这可能会推动它进行一次正则表达式替换。
我需要正则表达式方法,而不是 PHP 方法。假设我在文本编辑器或 JavaScript 中使用 regex-replace 来清理 PHP 源代码。