Is the "@" symbol sometimes used to surround a PHP regular expression? I'm working with a code base and found this function call:
$content = preg_replace("@(</?[^>]*>)+@", "", $content);
I believe it's removing all XML tags from the string but I'm not sure what the "@" symbol in there means.
The preg_replace() function returns a string or array of strings where all matches of a pattern or list of patterns found in the input are replaced with substrings. There are three different ways to use this function: 1. One pattern and a replacement string.
The ltrim() function removes whitespace or other predefined characters from the left side of a string. Related functions: rtrim() – Removes whitespace or other predefined characters from the right side of a string. trim() – Removes whitespace or other predefined characters from both sides of a string.
str_replace replaces a specific occurrence of a string, for instance "foo" will only match and replace that: "foo". preg_replace will do regular expression matching, for instance "/f. {2}/" will match and replace "foo", but also "fey", "fir", "fox", "f12", etc.
Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).
Yes, it can be used to wrap the expression. The original author most likely does this because some (or several) expressions contain the "more traditional" /
delimiter. By using @
, you can now use /
without the need to escape it.
You could use:
/pattern/flags
(traditional)@pattern@flags
$pattern$flags
The manual calls them the PCRE "delimiters". Any ASCII symbol (non-alphanumeric) can be used (except the null byte).
Common alternatives to /
are ~
and #
. But @
is just as valid.
PCRE also allows matching braces like (...)
or <...>
for the regular expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With