What Delimiter to use for preg_replace in PHP (replace working outside of PHP but not inside)
preg_replace()
requires a delimiter character:
preg_replace("/$pat/" ...
Traditionally it's the forward slash, but it can be any character - especially when you need the forward slash in the regex itself you can resort to another character.
This flexibility allows you to express "/http:\/\/foo\/bar\//"
("leaning toothpick syndrome") as "!http://foo/bar/!"
.
The delimiter character is necessary to separate the regex from the regex flags (a.k.a. "modifiers"), for example:
preg_replace("/$pat/i" ...
…this uses the i
flag to declare a case-insensitive regex.
preg_replace() warning : Delimiter must not be alphanumeric or backslash
String concatenation in php is handled with a period .
not a plus +
:
$pageParticulier->post_content = preg_replace(
"#>" . $old_instance['title'] . "<#",
">" . $new_instance['title'] . "<",
$pageParticulier->post_content
);
Warning: preg_replace(): No ending delimiter '/' found
Use str_replace
, or add delimiters to pattern if you really need preg_replace
.
$filesys = str_replace("/", "\\\\", $file);
OR
$filesys = preg_replace("~/~", "\\\\", $file);
What Delimiter to use for preg_replace in PHP (replace working outside of PHP but not inside)
preg_replace()
requires a delimiter character:
preg_replace("/$pat/" ...
Traditionally it's the forward slash, but it can be any character - especially when you need the forward slash in the regex itself you can resort to another character.
This flexibility allows you to express "/http:\/\/foo\/bar\//"
("leaning toothpick syndrome") as "!http://foo/bar/!"
.
The delimiter character is necessary to separate the regex from the regex flags (a.k.a. "modifiers"), for example:
preg_replace("/$pat/i" ...
…this uses the i
flag to declare a case-insensitive regex.
Using each match in preg_replace() in PHP
Like so:
$hola = 'yo';
$string = 'hello{{$hola}}hello{{$hola}}';
$result = preg_replace_callback('/\{\{\$(.*?)\}\}/', function ($matches) use ($hola) {
return ${$matches[1]};
}, $string);
var_dump($result);
preg_replace_callback
calls a callback on every match.
In order to use the $hola
variable inside the callback you need to explicitly make it available inside the function (use ($hola)
).
All this said... I don't get it. What this code does is essentially what PHP already does out-of-the-box.
$hola = 'yo';
$string = "hello{$hola}hello{$hola}";
echo $string; // "helloyohelloyo"
PHP, Regex - How to escape \n with preg_replace?
You don't need to use preg_replace
, you can use str_replace
, since you're not matching a pattern. And you need to put the subject string in single quotes, otherwise \n
will be treated as the escape sequence for newline.
str_replace('\\', '\\\\', '\n a');
See What is the difference between single-quoted and double-quoted strings in PHP?
Related Topics
PHP Random Shuffle Array Maintaining Key => Value
Hide Shipping Methods for Specific Shipping Class in Woocommerce
How to Group by and Sum PHP Array
Is PHP's Count() Function O(1) or O(N) for Arrays
How to Get the Os on Which PHP Is Running
Close Open HTML Tags in a String
File_Get_Contents - Failed to Open Stream: Http Request Failed! Http/1.1 404 Not Found
How to Delete an Object's Property in PHP
How to Get MAC Address of Client Using PHP
How to Set the Env Variable for PHP
How to Add a Delete Button to a PHP Form That Will Delete a Row from a MySQL Table
PHP Get Highest Value from Array