Vignesh Pichamani
Vignesh Pichamani

Reputation: 8070

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

Upvotes: 14

Views: 60408

Answers (5)

Srijeeta Ghosh
Srijeeta Ghosh

Reputation: 81

This worked for me!!

preg_replace("/\//", "", $input_lines);

Upvotes: 4

B.Asselin
B.Asselin

Reputation: 988

$str = preg_replace('/\\\\(.?)/', '$1', $str);

Upvotes: 3

nim
nim

Reputation: 507

You can also use stripslashes() like,

<?php echo stripslashes($var); ?>

Upvotes: 17

Bora
Bora

Reputation: 10717

To use backslash in replacement, it must be doubled (\\\\ PHP string) in preg_replace

echo preg_replace('/\\\\/', '', $var);

Upvotes: 22

roninblade
roninblade

Reputation: 1892

why would you use preg_replace when str_replace is much easier.

$str = str_replace('\\', '', $str);

Upvotes: 23

Related Questions