remove escape sequences from string in php

点点圈 提交于 2019-12-12 09:36:04

问题


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?


回答1:


The strip c slashes() function does exactly that:

stripcslashes('foo\r\n');



回答2:


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.



来源:https://stackoverflow.com/questions/7178399/remove-escape-sequences-from-string-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!