@ (at) symbol preg_replace function

Is the @ character used for regular expression php? I work with a code base and found this function call:

$content = preg_replace("@(</?[^>]*>)+@", "", $content);

I believe that it removes all the XML tags from the string, but I'm not sure what the @ symbol means.

+5
source share
4 answers

Yes, it can be used to wrap an expression. The original author most likely does this because some (or several) expressions contain a "more traditional" /delimiter. Using @, you can now use /without requiring its removal.

You can use:

  • /pattern/flags (traditional)
  • @pattern@flags
  • $pattern$flags
  • and etc.
+5
source

PCRE < ". ASCII ( -) ( ).

/ ~ #. @ .

PCRE , (...) <...> .

+2

PHP. . / ( ), , .

, %...%, ~...~ #...#, @...@ .

+1

The short answer is yes. The particular character used is not important; usually it should be something that does not appear in the template. For example, they are all equivalent:

<?php
    preg_replace("@(<\?[^>]*>)+@", "", $content);
    preg_replace("/(<\?[^>]*>)+/", "", $content);
    preg_replace("!(<\?[^>]*>)+!", "", $content);  
?>

The reason a character is needed is because modifiers can be added after the expression. For example, to search for a case insensitive case, you can use:

<?php
   preg_replace("@(<\?[^>]*>)+@i", "", $content);
?>
+1
source

All Articles