Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP preg_replace \

Really simple question: how can I preg_replace the backslash character?

like image 757
Oliver Bayes-Shelton Avatar asked Feb 08 '10 15:02

Oliver Bayes-Shelton


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.

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 do you replace words in regex?

Word supports find/replace with it own variation of regular expressions (regex), which is called wildcards. To use regex: Ctrl-H (Find/Replace) ⇒ Check "Use wildcards" option under "More".

Which function is used to replacing pattern in string?

The REGEXREPLACE( ) function uses a regular expression to find matching patterns in data, and replaces any matching values with a new string.


1 Answers

Yes, but you need to escape it. When using it in the regexp use \\ to use it in the replacement, use \\\\ (that will turn into \\ that will be interpreted as a single backslash).

like image 152
Johnco Avatar answered Oct 08 '22 23:10

Johnco