转义数组值中的引号或特殊字符
在我的 PHP 代码中,我设置了一个区域,供人们输入要显示的自己的信息。信息存储在一个数组中,我想让它尽可能灵活。
如果我有类似...
$myArray[]['Text'] = 'Don't want this to fail';
或
$myArray[]['Text'] = "This has to be "easy" to do";
我将如何转义数组值中的撇号或引号?
谢谢
编辑:由于只有一对一的关系,我将数组更改为这种结构...
$linksArray['Link Name'] ='/path/to/link';
$linksArray['Link Name2'] ='/path/to/link2';
$linksArray['Link Name2'] ='/path/to/link3';
计划是我设置一个包含文件的模板,其中包含其他人(技术水平较低的人)格式的这些链接可以维持。他们将直接访问 PHP,我担心他们可能会在“链接名称”区域中放置单引号或双引号并破坏系统。
再次感谢。
可能的解决方案:
谢谢@Tim Cooper。
这是一个对我有用的示例......
$link = "http://www.google.com";
$text = <<<TEXT
Don't you loving "googling" things
TEXT;
$linksArray[$text] = $link;
In my PHP code, I'm setting up an area for people to enter their own info to be displayed. The info is stored in an array and I want to make it as flexible as possible.
If I have something like...
$myArray[]['Text'] = 'Don't want this to fail';
or
$myArray[]['Text'] = "This has to be "easy" to do";
How would I go about escaping the apostrophe or quote within the array value?
Thanks
Edit: Since there is only a one to one relationship, I changed my array to this structure...
$linksArray['Link Name'] ='/path/to/link';
$linksArray['Link Name2'] ='/path/to/link2';
$linksArray['Link Name2'] ='/path/to/link3';
The plan is I set up a template with an include file that has these links in a format someone else (a less technical person) can maintain. They will have direct access to the PHP and I'm afraid they may put a single or double quote in the "link name" area and break the system.
Thanks again.
POSSIBLE SOLUTION:
Thanks @Tim Cooper.
Here's a sample that worked for me...
$link = "http://www.google.com";
$text = <<<TEXT
Don't you loving "googling" things
TEXT;
$linksArray[$text] = $link;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 heredoc 可能是一个不错的选择解决方案:
Using a heredoc might be a good solution:
PHP 将在输入时正确处理这些字符串。
如果您按照所示方式自己构建字符串,则可以在引号样式(单引号和双引号)之间切换...如下所示:
或者,如果必须转义字符,请在引号前使用 \ 字符。
PHP will process these strings properly upon input.
If you are constructing the strings yourself as you have shown, you can alternate between quotation styles (single and double)...as in:
Or, if you must escape the characters, you use the \ character before the quotation.
如果您确实想让 i 变得简单,请使用 INI 或 XML 样式的单独配置文件。 INI 通常是人们最容易手动编辑的。如果您有真正的嵌套结构,XML 就很好。
If you really want to make i easy, use a separate configuration file in either INI or XML style. INI is usually the easiest for people to edit manually. XML is good if you have a really nested structure.
除非您让用户直接输入 PHP 代码(您可能不会),否则您不必担心他们输入的内容,直到您显示它为止。当您实际显示他们输入的信息时,您将需要使用诸如
htmlentities()
之类的东西对其进行清理。编辑:我意识到我可能误解了你的问题。如果是这样,请忽略它! :)
Unless you are letting users enter direct PHP code (you probably aren't), you don't have to worry about what they enter until you go to display it. When you actually display the info they enter, you will want to sanitize it using something like
htmlentities()
.Edit: I realize I may be misunderstanding your question. If so, ignore this! :)
您可以使用addslashes($str)函数自动转义引号。
您还可以尝试 htmlentities,它将引号和其他特殊值编码为 HTML 实体: http:// /php.net/manual/en/function.htmlentities.php
You can use the addslashes($str) function to automatically escape quotes.
You can also try htmlentities, which will encode quotes and other special values into HTML entities: http://php.net/manual/en/function.htmlentities.php