简体   繁体   中英

preg_match_all() php tags that aren't inside quotes

This question is Part 2 of PHP's preg_match_all() to pull out all php tags

I need to extend this regex /<\\?.*?(?:\\?>|$)/s to not match when it finds tags inside single or double quotes. This will involve back-referencing the matched quote type, which goes beyond my intuitive understanding of regex.


Example 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";
?>

Hoped for Result:

[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";
?>"

If you're trying to use PHP to parse PHP, you may want to try using the token_get_all() function. It'll do a lot of the work for you, using the same engine that PHP uses to parse code that it's running.

For future reference: /<\\?(?:.*?(?:(\\"|').*?[^\\\\]\\1)*)*(?:\\?>|$)/s

Work perfectly with all of my unit tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM