转义数组值中的引号或特殊字符

发布于 2024-11-04 19:51:16 字数 809 浏览 1 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

想你只要分分秒秒 2024-11-11 19:51:16

使用 heredoc 可能是一个不错的选择解决方案:

$myArray[]['Text'] = <<<TEXT

Place text here without escaping " or '

TEXT;

Using a heredoc might be a good solution:

$myArray[]['Text'] = <<<TEXT

Place text here without escaping " or '

TEXT;
画离情绘悲伤 2024-11-11 19:51:16

PHP 将在输入时正确处理这些字符串。
如果您按照所示方式自己构建字符串,则可以在引号样式(单引号和双引号)之间切换...如下所示:

$myArray[]['Text'] = "Don't want this to fail";
$myArray[]['Text'] = 'This has to be "easy" to do';

或者,如果必须转义字符,请在引号前使用 \ 字符。

$myArray[]['Text'] = 'Don\'t want this to fail';
$myArray[]['Text'] = "This has to be \"easy\" to do";

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:

$myArray[]['Text'] = "Don't want this to fail";
$myArray[]['Text'] = 'This has to be "easy" to do';

Or, if you must escape the characters, you use the \ character before the quotation.

$myArray[]['Text'] = 'Don\'t want this to fail';
$myArray[]['Text'] = "This has to be \"easy\" to do";
最美的太阳 2024-11-11 19:51:16

如果您确实想让 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.

江湖正好 2024-11-11 19:51:16

除非您让用户直接输入 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! :)

银河中√捞星星 2024-11-11 19:51:16

您可以使用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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文