Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove backslash \ from string using preg replace of php

I want to remove the backslash alone using php preg replace.

For example: I have a string looks like

$var = "This is the for \testing and i want to add the # and remove \slash alone from the given string";

How to remove the \ alone from the corresponding string using php preg_replace

like image 303
Vignesh Pichamani Avatar asked Aug 30 '13 07:08

Vignesh Pichamani


People also ask

How do you remove a backward slash from a string?

replace() method to remove all backslashes from a string, e.g. const replaced = str. replace(/\\/g, ''); . The String. replace() method will remove all backslashes from the string by replacing them with empty strings.

How do I strip a slash in PHP?

The stripslashes() function removes backslashes added by the addslashes() function. Tip: This function can be used to clean up data retrieved from a database or from an HTML form.

What is the difference between Preg_replace and Str_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.

Why does PHP use backslash?

Escape Sequences In PHP, an escape sequence starts with a backslash \ . Escape sequences apply to double-quoted strings. A single-quoted string only uses the escape sequences for a single quote or a backslash.


1 Answers

You can also use stripslashes() like,

<?php echo stripslashes($var); ?>
like image 153
nim Avatar answered Oct 03 '22 13:10

nim