Page 1 of 1

Dealing with & and other chars in a string

Posted: Sat Jan 28, 2012 4:18 pm
by pizzipie
I am trying to create a form on the fly and data input that includes & does not show up.
PHP_SpclChars.png
The array is the input with output form below.

I've tried using htmlspecialchars() as below but it doesn't work. I'm using incorrectly somehow eh!

Thanks for your help in advance.

RP.

Code: Select all

foreach($connectData as $key => $value) {
	if($key==Name_ID) continue;
		printf("<tr>\n");
		printf("<td>\n");
		printf("<label for=%s>%s</label>\n",strtolower($key), $key);
		printf("<input type=text name=%s id=%s value=%s>\n", strtolower($key), strtolower($key), htmlspecialchars($value));
		printf("<td>\n");
		printf("<tr>\n");
		
	}
	


Re: Dealing with & and other chars in a string

Posted: Sat Jan 28, 2012 6:39 pm
by Vegan
use the standard C escape codes for the printf function

they all use \ to prefix them, such as \\ which will print a single backslash etc

Re: Dealing with & and other chars in a string

Posted: Sat Jan 28, 2012 8:18 pm
by twinedev
The problem has nothing to do with the & character, it is that you are outputting poor HTML code by not wrapping the attribue values with double quotes, so when it hits a space, since the browser is trying to guess at what you mean, it is guessing the space starts a new attribute:

This is most likely the output you are getting:

Code: Select all

<input type=text name=first_name id=first_name value=Frank & Doady>
as you can see, this in invalid HTML code. You need it to generate this code:

Code: Select all

<input type="text" name="first_name" id="first_name" value="Frank & Doady">
The talk about escape codes is irrellivant to the issue (and in fact you are using correct escape codes).

-Greg

Re: Dealing with & and other chars in a string

Posted: Sat Jan 28, 2012 10:46 pm
by pizzipie
:D :D :D :D :D :D SOLVED:

Thanks twinedev! I knew I had to be doing something dumb. I needed another pair of eyes to see it.

RP