Remove escape sequences from string in php

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

I used stripslashes() , which correctly cancels one or two quotes, but does not touch \r\n .

I am worried that there are other escape sequences of characters that I don't know. Is there a function that I can use that will give me the true length of the string, as it would be in the database? If I need to create my own function, what other sequences should handle?

+7
string php escaping
Aug 24 '11 at 15:47
source share
2 answers

The strip function with a slash () does just that:

 stripcslashes('foo\r\n'); 
+8
Aug 24 '11 at 15:55
source share

You can use substr_count() to count the characters in a string. Just count the number of backslashes in a string:

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

This will give you a rough estimate. To be 100% accurate, you would need to count the number of double backslashes to allow for literal backslashes, and adjust the counter accordingly.

+2
Aug 24 '11 at 15:54
source share



All Articles