Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove escape sequences from string in php

I'm working with a mysqldump file that has escaped character sequences. I need to know the length of a string as its database value, but the dump has escape characters in it, which add length to the string.

I've used stripslashes() which properly un-escapes single- and double-quotes, but it doesn't touch the \r\n.

I'm concerned there are other escaped character sequences in there that I'm not aware of. Is there a function I can use that will give me the true length of the string as it would be in the database? If I have to build my own function, what other sequences should it handle?

like image 464
user151841 Avatar asked Aug 24 '11 15:08

user151841


People also ask

What is the function that can be used to remove escape characters in a string?

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 (” “). Example: This example illustrates the use of the str_replace() function to remove the special characters from the string.

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 $.

What is \r escape sequence in Java?

Java code for the escape sequence \r: // This \r escape sequence is a carriage return character. // It moves the output point back to the beginning of the line without moving down a line (usually). public class Test { public static void main(String[] args)


2 Answers

The strip c slashes() function does exactly that:

stripcslashes('foo\r\n');
like image 94
Arnaud Le Blanc Avatar answered Oct 13 '22 11:10

Arnaud Le Blanc


You can use substr_count() to count characters in a string. Simply count how many backslashes are in the string:

$string = "... mysqldump string here ...";
$backslashes = substr_count($string, '\\');

That'll give you a rough count. To be 100% accurate, you'd have to count how many double backslashes there are, to account for literal backslashes, and adjust the count as appropriate.

like image 26
Marc B Avatar answered Oct 13 '22 11:10

Marc B