Forum Moderators: coopster

Message Too Old, No Replies

space messup.

         

snehula

10:39 am on Aug 3, 2011 (gmt 0)

10+ Year Member



Hi there,

I dunno if it's the grey skies or the flu that's giving me grief trying to figure this out today..

problem is the following:

I have a name coming from an <option> tag in my $_POST array. The name is for instance $_POST[name] = "Paris Hilton"; The way I populated the options in this list is I retrieved firstname and surname from a db table and set the <option> tag value to be $firstname."&nbsp;".$surname

Now, on form submit, I'm trying to split the name into firstname and surname again. I tried explode(" ",$_POST['name']), nothing happened, it stayed in one piece. I tried explode("&nbsp;", $_POST['name']), same result. I said to myself, okay, this is the first time I'm trying to use explode, maybe I should go with something mroe familiar to figure out where the problem is. So I tried strpos to look for that space.
strpos($_POST['name'], ' ') returns absolutely nothing, same with strpos($_POST['name'],'&nbsp;'). What happened to the space? Why is it invisible to any function I'm trying to find it with? Do I need to escape it somehow in the function parameter list?

snehula

11:37 am on Aug 3, 2011 (gmt 0)

10+ Year Member



alrite i found a better solution than what i was trying to do here, which obliterates the need to use explode or anything like that, but all in all, i am curious about where that space is hiding or what is the problem here..

penders

11:50 am on Aug 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When you put "&nbsp;" in the HTML source, the browser converts this into a non-breaking-space character (char code 160), not an ordinary space (char code 32). So, a string with char#160 is being sent back with your form data. "&nbsp;" is just a series of 6 characters as far as PHP is concerned.

So, you could use explode() on char#160 (A0 in hex) ...
explode("\xA0",$_POST['name']);


But, as you suggest, best avoid &nbsp; in the value in the first place. (eg. Use a code that you lookup in your database in order to validate the submitted data.)

snehula

1:17 pm on Aug 3, 2011 (gmt 0)

10+ Year Member



thanks a million :-)

d3vrandom

2:05 pm on Aug 3, 2011 (gmt 0)

10+ Year Member



You could also try the value attribute of the option tag. Anything you put in it will be passed back to the server on POST. This way you can put machine readable stuff into the value attribute for the server and have the human readable text shown to the user:


<option value="asterixandobelix">Asterix and obelix</option>

snehula

2:46 pm on Aug 3, 2011 (gmt 0)

10+ Year Member



that's exactly what i did :-) thanxx