Home > Backend Development > PHP Tutorial > How Can I Efficiently Match Quoted Strings in PHP, Ignoring Escaped Quotes?

How Can I Efficiently Match Quoted Strings in PHP, Ignoring Escaped Quotes?

Mary-Kate Olsen
Release: 2024-11-29 13:01:10
Original
769 people have browsed it

How Can I Efficiently Match Quoted Strings in PHP, Ignoring Escaped Quotes?

PHP: Matching Strings While Ignoring Escaped Quotes Within Quotes

To match strings enclosed within both single (') and double (") quotes, you can use the following regular expressions:

<br>$code = preg_replace_callback( '/"(.*?)"/', array( &$this, '_getPHPString' ), $code );</p>
<p>$code = preg_replace_callback( "#'(.*?)'#", array( &$this, '_getPHPString' ), $code );<br>

However, these expressions do not account for escaped quotes within the strings. To ignore escaped quotes, you can use more sophisticated regular expressions:

Good (But Inefficient):

<br>"(<sup><a href="https://www.php.cn/link/d58f36f7679f85784d8b010ff248f898" rel="nofollow" target="_blank">1</a></sup>|.)*"<br>

Better (More Efficient with Special Quantifiers):

<br>"(<sup><a href="https://www.php.cn/link/960fe54b16d890a75e845fcd23afc32d" rel="nofollow" target="_blank">2</a></sup>  |.)*"<br>

Best (Most Efficient with Unrolled Loop):

<br><sup><a href="https://www.php.cn/link/84fec9a8e45846340fdf5c7c9f7ed66c" rel="nofollow" target="_blank">3</a></sup><em>(?:.<sup><a href="https://www.php.cn/link/9b1cab1b93285ce58e7c1dc576ff8a14" rel="nofollow" target="_blank">4</a></sup></em>)*<br>

These improved expressions will allow escaped quotes (' and ") to be ignored within their respective quoted strings. For PHP syntax, use the following:

<br>$re_dq = '/"<sup><a href="https://www.php.cn/link/b594f8f8fcc3cc7910e2dcd4269a2e95" rel="nofollow" target="_blank">5</a></sup><em>(?:\.<sup><a href="https://www.php.cn/link/096ce33c96792e289516407eb29b62bb" rel="nofollow" target="_blank">6</a></sup></em>)*"/s';<br>$re_sq = "/'<sup><a href="https://www.php.cn/link/57947ed4d4130c7ff0a057c8654dd1a3" rel="nofollow" target="_blank">7</a></sup><em>(?:\.<sup><a href="https://www.php.cn/link/7835a9ef21ac8378a23835829594d598" rel="nofollow" target="_blank">8</a></sup></em>)*'/s";<br>

These regular expressions provide accurate and efficient matching of strings, even when dealing with escaped quotes.


  1. "
  2. "
  3. "
  4. "
  5. "\
  6. "\
  7. '\
  8. '\

The above is the detailed content of How Can I Efficiently Match Quoted Strings in PHP, Ignoring Escaped Quotes?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template