tags:

views:

145

answers:

2

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.

Thanks

+3  A: 

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;
pixeline
This is a valid work around. I already use the value field for quantity but Yes, I could make a two dimensional array. I would like to know if the problem is solvable somehow having it read as a string literal before I look to alternatives. Thanks for the input though, every bit helps!
payling
A: 

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

payling
Please update the question so that others searching SO with a similar problem will find this solution.
Lucas Oman
Stackoverflow says not to change the original question. The original question is what I thought to be wrong, the bottom answer is how it was solved.
payling