Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove escape characters using PHP?

Tags:

php

escaping

I have the following text

$text = "\\vct=140\\Araignée du soir espoir,araignée du matin,espoir du matin."

I want to remove the escape characters using php..I do not want to use stripslashes since it is server dependent..

I want to remove the '\' before the '\vct=140' and the '\' before '\Arai....' How can I remove them from the string?

like image 563
user954687 Avatar asked Oct 13 '11 05:10

user954687


People also ask

How do I remove escape characters from data in PHP?

We have given a string and we need to remove special characters from string str in PHP, for this, we have the following methods in PHP: Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).

How do I ignore an escape character in a string?

To ignoring escape sequences in the string, we make the string as "raw string" by placing "r" before the string. "raw string" prints as it assigned to the string.

How can I replace special characters in a string in PHP?

The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.

What are the four ways of escaping to PHP?

Widely used Escape Sequences in PHP\' – To escape ' within the single-quoted string. \” – To escape “ within the double-quoted string. \\ – To escape the backslash. \$ – To escape $.


1 Answers

As far as I can tell stripslashes works as per the manual:

<?php
$string = '\\\\vct=140\\\\Araignée du soir espoir.';
echo $string, "\n";
echo stripslashes($string), "\n";

Which outputs:

\\vct=140\\Araignée du soir espoir.
\vct=140\Araignée du soir espoir.

Sequence \\ is in list of escape sequences for special characters so you got to escape slashes twice.

like image 164
sanmai Avatar answered Oct 05 '22 09:10

sanmai