Preg_match_all()引号内没有't的PHP标签


preg_match_all() php tags that aren't inside quotes

这个问题是php 's preg_match_all()的第2部分,用于提取所有php标签

我需要扩展这个正则表达式/<'?.*?(?:'?>|$)/s,当它在单引号或双引号内找到标签时不匹配。这将涉及到反向引用匹配的引号类型,这超出了我对正则表达式的直观理解。


HTML例子:

<?
  // Test xml
  $this->_xml = "<?xml version='"1.0'" encoding='"UTF-8'"?>'n";
  $this->_xml .= "<TransferredValueTxn xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance'" >'n";
  $this->_xml .= "<?=$test?>    <TransferredValueTxnReq>" . trans("test") . "'n";
?>

期望结果:

[0] => "<?
  // Test xml
  $this->_xml = "<?xml version='"1.0'" encoding='"UTF-8'"?>'n";
  $this->_xml .= "<TransferredValueTxn xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance'" >'n";
  $this->_xml .= "<?=$test?>    <TransferredValueTxnReq>" . trans("test") . "'n";
?>"

如果您试图使用PHP来解析PHP,您可能想尝试使用token_get_all()函数。它会为你做很多工作,使用PHP用来解析它正在运行的代码的引擎。

供今后参考:/<'?(?:.*?(?:('"|').*?[^'']'1)*)*(?:'?>|$)/s

与我所有的单元测试完美配合

相关文章: