php string escaping like python's “”“ ”“”?

我怕爱的太早我们不能终老 提交于 2019-12-31 02:11:03

问题


Hi I was wondering if there is an easy way to escape strings in php.

In python I use """ """, and everything between there is escaped. so when using special characters it is ignored.

I have some text to echo, and escaping everything manually just takes forever.

Does php have a similar function built in ?

thanks!


回答1:


Which are the characters do you have to escape?

You could use single quotes [docs]. The only characters that have to be escaped in such a string are \ and '.

If you have a long string, also have a look at heredoc [docs].




回答2:


Since PHP 5.3, you can use nowdoc. As opposed to heredoc, nowdoc does not expand variables inside it.




回答3:


There are various functions depending on what you want to escape.

If you are using a lot of double quotes, for example with html, you can wrap the string in single quotes to prevent having to escape.

$string = '<a href="#">no escape needed</a>';

The same goes the other way

$string = "I'd rather be gaming";

Then you have a couple of functions used mostly for escaping user input:

addslashes() that will escape quotes
htmlspecialchars() will 'escape' html codes
mysql_real_escape_string() for escaping mysql input



来源:https://stackoverflow.com/questions/6556178/php-string-escaping-like-pythons

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