TeamStar TeamStar - 2 months ago 48
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

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.


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

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