What Delimiter to Use for Preg_Replace in PHP (Replace Working Outside of PHP But Not Inside)

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



Leave a reply



Submit