php从数组中删除带有句点的项目

发布于 2024-08-22 16:43:22 字数 227 浏览 4 评论 0原文

我有一系列小部件的产品。

某些小部件的名称中包含保留的句点符号。

当php遇到句点时就会出现问题,小部件名称的其余部分将被忽略。我怎样才能将小部件作为字符串文字读取,以便句点符号不会干扰?

unset($products[$widget]);
                  

我可以用占位符字母替换句点,这样它就不是保留字符,但我不想这样做。

I have a products array of widgets.

Some widgets have the reserved period symbol in their names.

The problem occurs when php meets the period, the rest of the widget name is disregarded. How could I read the widget in as a string literal so that the period symbol will not interfere?

unset($products[$widget]);
                  

I could replace the period with a placeholder letter so that it's not a reserved character, but I'd prefer not to.

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

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

发布评论

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

评论(2

殊姿 2024-08-29 16:43:22

使用其名称的规范化值作为键,使用真实的小部件名称作为值。

例子:

function slug($str)
{
    $str = strtolower(trim($str));
    $str = preg_replace('/[^a-z0-9-]/', '-', $str);
    $str = preg_replace('/-.+/', "-", $str);
    return $str;
}

$products[slug($widget)] = $widget;

use a normalized value of their name as key, and the real widget name as value.

Example:

function slug($str)
{
    $str = strtolower(trim($str));
    $str = preg_replace('/[^a-z0-9-]/', '-', $str);
    $str = preg_replace('/-.+/', "-", $str);
    return $str;
}

$products[slug($widget)] = $widget;
囍孤女 2024-08-29 16:43:22

我做了一些错误的假设。小部件名称还包含井号符号,我没有提及这些符号,因为我不知道 # 符号以它们的方式影响代码。

但事实证明,英镑符号毕竟导致了问题,但在使用 unset 命令时却没有。

我使用 $_GET[''] 通过 URL 传递了小部件的名称。显然,接收 get 不喜欢 # 符号,并删除 # 符号之后的所有内容。

这是一个快速解释。
Soo…假设我正在传递小部件名称“crank#x.55”。
如果我回显 $_GET 变量,您会看到“crank”,并且剩余的“#x.55”被剥离。

当我查看 url 时,它显示“crank#x.55”(完整的小部件名称),这让我相信它已正确发送到 $_GET var。当我执行 unset 操作时,我认为句点(连接)符号正在做一些奇怪的事情,所以这就是我创建原始问题的原因。 (我知道我需要为将来的问题提供更多事实)<--学习过程。

我通过简单地将磅符号替换为 @ 符号来解决这个问题,然后在发布数据后使用 preg_replace 将 @ 符号转换回磅。与 Pixeline 的答案类似,只是根据我的目的进行了一些调整。

谢谢

I made some assumptions that were wrong. The widget name’s also contains pound symbols which I lacked to mention because I didn't know that # symbols effected code the way they do.

But as it turns out the pound symbol caused the problem after all, but not when using the unset command.

I passed the widget’s name through the URL using $_GET[‘’]. Apparently the receiving get does not like # symbol and removes everything after pound # symbols.

Here's a quick explanination.
Soo… let’s say I was passing the widget name of “crank#x.55”.
If I echo’ed the $_GET variable you’d see "crank" and the remaining "#x.55" is stripped off.

When I looked in the url it showed "crank#x.55" (the full widget name) which lead me to believe that it was sent to the $_GET var correctly. I thought the period (concatination) symbol was doing something screwy when I peformed unset so that is why I created the original question. (i know I need to include more facts for future questions) <-- learning process.

I solved the problem by simply replacing pound symbols with @ symbols and then once the data has been posted convert the @ symbol back to a pound using preg_replace. Similar to pixeline’s answer just adapted a bit for my purpose.

Thanks

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