Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ (at) symbols preg_replace function

Tags:

regex

php

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.

like image 855
patorjk Avatar asked Aug 09 '11 15:08

patorjk


People also ask

What does Preg_replace do in PHP?

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.

How do I remove a character from a string in laravel?

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.

What is the difference between Str_replace and Preg_replace?

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.

How to Remove special character in string PHP?

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 (” “).


2 Answers

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
  • etc.
like image 138
Brad Christie Avatar answered Sep 17 '22 05:09

Brad Christie


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.

like image 25
mario Avatar answered Sep 20 '22 05:09

mario