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?
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.
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 $.
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)
The strip c slashes() function does exactly that:
stripcslashes('foo\r\n');
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With