[英]preg_match_all() php tags that aren't inside quotes
这个问题是PHP 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
:. /<\\?(?:.*?(?:(\\"|').*?[^\\\\]\\1)*)*(?:\\?>|$)/s
:( /<\\?(?:.*?(?:(\\"|').*?[^\\\\]\\1)*)*(?:\\?>|$)/s
: /<\\?(?:.*?(?:(\\"|').*?[^\\\\]\\1)*)*(?:\\?>|$)/s
与我的所有单元测试完美配合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.