TeamStar TeamStar - 1 month ago 25
PHP Question

PHP preg_replace backslash

I have double backslashes '\' in my string that needs to be converted into single backslashes '\'. I've tried several combinations and end up with the whole string disappearing when I used echo or more backslashes are added to the string by accident. This regex thing is making me go bonkers...lol...

I tried this amongst other failed attempts:

$pattern = '[\\]';
$replacement = '/\/';

?>
<td width="100%">&nbsp;<?php echo preg_replace($pattern, $replacement,$q[$i]);?></td>


I do apologise if this is a foolish issue and I appreciate any pointers.

Answer

Use stripslashes() - it does exactly what you're looking for.

<td width="100%">&nbsp;<?php echo stripslashes($q[$i]);?></td>