PHP - 在永远不能由用户定义的字符上爆炸......如何?

发布于 2024-12-20 14:44:37 字数 742 浏览 0 评论 0原文

好的,我试图找到一个字符或一组字符,或者可以使用的东西,我可以从它爆炸,因为文本是用户定义的,我需要能够从我拥有的永远无法爆炸的值爆炸文本内。

我该怎么做?

我正在尝试做的一个例子...

$value = 'text|0||#fd9||right';

好吧,

文本在这里永远不应该改变。 0,同样不可更改 #fd9 是用户定义的字符串,可以是用户输入的任何内容... right 设置方向(左或右)。

所以,我面临的问题是:如何 explode("||", $value) 以便如果存在 ||在用户定义的部分内...示例:

$value = 'text|0||Just some || text in here||right';

因此,如果用户放置 ||在字符串的用户定义部分中,这会搞砸。无论用户在字符串中输入什么内容,如何做到这一点?这样它应该返回以下数组:

array('text|0', 'Just some || text in here', 'right');

我应该使用不同的字符来爆炸吗?如果是这样,我可以使用什么用户将无法输入到字符串中,或​​者如何检查并修复它?我可能不应该使用 ||在这种情况下,但我可以用什么来解决这个问题?

此外,该值首先来自字符串,然后来自数据库(保存后)。

有什么想法吗?

Ok, am trying to find a character or group of characters, or something that can be used that I can explode from, since the text is user-defined, I need to be able to explode from a value that I have that can never be within the text.

How can I do this?

An example of what I'm trying to do...

$value = 'text|0||#fd9||right';

Ok,

text is something that should never change in here.
0, again not changeable
#fd9 is a user-defined string that can be anything that the user inputs...
and right sets the orientation (either left or right).

So, the problem I'm facing is this: How to explode("||", $value) so that if there is a || within the user-defined part... Example:

$value = 'text|0||Just some || text in here||right';

So, if the user places the || in the user-defined part of the string, than this messes this up. How to do this no matter what the user inputs into the string? So that it should return the following array:

array('text|0', 'Just some || text in here', 'right');

Should I be using different character(s) to explode from? If so, what can I use that the user will not be able to input into the string, or how can I check for this, and fix it? I probably shouldn't be using || in this case, but what can I use to fix this?

Also, the value will be coming from a string at first, and than from the database afterwards (once saved).

Any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

懷念過去 2024-12-27 14:44:37

如何将任意数据类型表示为字符串的问题总是遇到您所描述的问题,并且已经通过多种方式解决了这个问题。这个过程称为序列化,有很多种序列化格式,任何 PHP 原生的 serializeJSON 到 XML。所有这些格式都指定如何将复杂的数据结构表示为字符串,包括如何在序列化值本身的序列化格式中使用具有特殊含义的字符的转义规则。


来自评论:

好吧,基本上,这很简单。我已经概述了 13 个其他参数以及它们在位于此处的 Dream Portal 中的工作方式:http://dream -portal.net/topic_122.0.html 所以,你可以看到它们是如何适应的。我正在研究一个字段集参数,它基本上使用所有这些参数,而不是一些将多个参数包含到 1 中。无论如何, 希望该链接可以帮助您了解模块的 XML 文件是什么样子: http:// dream-portal.net/topic_98.0.html 查看 info.xml 部分,注意底部的 标记,其中有 2 个。

在我看来,更合理地使用 XML 将使这变得容易得多。我还没有详细阅读整个内容,但是像这样的 XML 元素

<param name="test_param" type="select">0:opt1;opt2;opt3</param>

会更有意义,写为

<select name="test_param">
    <option default>opt1</option>
    <option>opt2</option>
    <option>opt3</option>
</select>

每个唯一的配置选项都可以有自己唯一的元素命名空间,其中包含自定义子元素,具体取决于您需要表示的参数类型。这样就不需要为每个可能的参数发明一个自定义的迷你格式。它还允许您创建正式的 XML 架构(这是否对您有任何好处取决于另一个主题,但至少您正在使用 XML,因为它应该被使用)。

The problem of how to represent arbitrary data types as strings always runs up against exactly the problem you're describing and it has been solved in many ways already. This process is called serialization and there are many serialization formats, anything from PHP's native serialize to JSON to XML. All these formats specify how to present complex data structures as strings, including escaping rules for how to use characters that have a special meaning in the serialization format in the serialized values themselves.


From the comments:

Ok, well, basically, it's straight forward. I already outlined 13 of the other parameters and how they work in Dream Portal located here: http://dream-portal.net/topic_122.0.html so, you can see how they fit in. I'm working on a fieldset parameter that basically uses all of these parameters and than some to include multiple parameters into 1. Anyways, hope that link helps you, for an idea of what an XML file looks like for a module: http://dream-portal.net/topic_98.0.html look at the info.xml section, pay attention to the <param> tag in there, at the bottom, 2 of them.

It seems to me that a more sensible use of XML would make this a lot easier. I haven't read the whole thing in detail, but an XML element like

<param name="test_param" type="select">0:opt1;opt2;opt3</param>

would make much more sense written as

<select name="test_param">
    <option default>opt1</option>
    <option>opt2</option>
    <option>opt3</option>
</select>

Each unique configuration option can have its own unique element namespace with custom sub-elements depending on the type of parameter you need to represent. Then there's no need to invent a custom mini-format for each possible parameter. It also allows you to create a formal XML schema (whether this will do you any good or not is a different topic, but at least you're using XML as it was meant to be used).

若无相欠,怎会相见 2024-12-27 14:44:37

您可以将任何用户输入编码为 base64,然后将其用于爆炸或您希望的任何方式。

print base64_encode("abcdefghijklmnopqrstuvwxyz1234567890`~!@#$%^&*()_+-=[];,./?>:}{<");

序列化数组也不是一个坏主意。它可能比使用逗号分隔的字符串和爆炸更好。 Drupal 充分利用了序列化数组。

查看 PHP 手册了解如何使用它:

  • serialize()
  • < a href="http://www.php.net/manual/en/function.unserialize.php" rel="nofollow">unserialize()

You can encode any user input to base64 and then use it with explode or however you wish.

print base64_encode("abcdefghijklmnopqrstuvwxyz1234567890`~!@#$%^&*()_+-=[];,./?>:}{<");

serialized arrays are also not a bad idea at all. it's probably better than using a comma separated string and explode. Drupal makes good use of serialized arrays.

take a look at the PHP manual on how to use it:

浅听莫相离 2024-12-27 14:44:37

编辑:新解决方案

是否保证 text 不包含 || 本身?
如果没有,您可以将 substr()strpos()strrpos() 结合使用,而不是爆炸


这是我通常的做法解决这个问题。

1) 捕获用户的文本并将其保存在 var $user_text 中;

2) 在 $user_text 上运行 str_replace() 来替换您想要分割的字符:

//replace with some random string the user would hopefully never enter 
$modified = str_replace('||','{%^#',$user_text);

3) 现在您可以安全地分解您的文本使用 ||

4) 现在在爆炸的每个部分运行 str_replace,将其设置回原始用户输入的文本

foreach($parts as &$part) {
    $part = str_replace('{%^#','||',$part);
}

EDIT: New Solution

Is it a guarantee that text doesn't contain || itself?
If it doesn't, you can use substr() in combination with strpos() and strrpos() instead of explode


Here's what I usually do to get around this problem.

1) capture user's text and save it in a var $user_text;

2) run an str_replace() on $user_text to replace the characters you want to split by:

//replace with some random string the user would hopefully never enter 
$modified = str_replace('||','{%^#',$user_text);

3) now you can safely explode your text using ||

4) now run an str_replace on each part of the explode, to set it back to the original user entered text

foreach($parts as &$part) {
    $part = str_replace('{%^#','||',$part);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文