简体   繁体   English

php 字符串 escaping 像 python 的“”“”“”?

[英]php string escaping like python's “”“ ”“”?

Hi I was wondering if there is an easy way to escape strings in php.嗨,我想知道是否有一种简单的方法可以在 php 中转义字符串。

In python I use """ """, and everything between there is escaped.在 python 中,我使用“”“”“”,并且之间的所有内容都被转义了。 so when using special characters it is ignored.所以当使用特殊字符时,它会被忽略。

I have some text to echo, and escaping everything manually just takes forever.我有一些文本要回显,而 escaping 手动操作一切都需要永远。

Does php have a similar function built in? php 是否内置了类似的 function?

thanks!谢谢!

Which are the characters do you have to escape?您必须逃脱哪些角色?

You could use single quotes [docs] .您可以使用单引号[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] .如果你有一个长字符串,也看看heredoc [docs]

Since PHP 5.3, you can use nowdoc .从 PHP 5.3 开始,您可以使用nowdoc As opposed to heredoc, nowdoc does not expand variables inside it.与heredoc 不同,nowdoc 不会在其中扩展变量。

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.如果您使用大量双引号,例如 html,您可以将字符串括在单引号中以防止转义。

$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:然后你有几个主要用于 escaping 用户输入的函数:

addslashes() that will escape quotes addlashes()将转义引号
htmlspecialchars() will 'escape' html codes htmlspecialchars()将“转义” html 代码
mysql_real_escape_string() for escaping mysql input mysql_real_escape_string()用于 escaping mysql 输入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM