문제

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

도움이 되었습니까?

해결책 2

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

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

다른 팁

why would you use preg_replace when str_replace is much easier.

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

You can also use stripslashes() like,

<?php echo stripslashes($var); ?>
$str = preg_replace('/\\\\(.?)/', '$1', $str);

This worked for me!!

preg_replace("/\//", "", $input_lines);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top