开发者

php removing item with period from array

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.


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;


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜