Dealing with & and other chars in a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Dealing with & and other chars in a string

Post 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");
		
	}
	

User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: Dealing with & and other chars in a string

Post 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
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Dealing with & and other chars in a string

Post 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
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: Dealing with & and other chars in a string

Post 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
Post Reply