Page 1 of 1

Problem with Ampersand (&) in array

Posted: Wed May 13, 2009 6:13 pm
by AAKAlan
I have a very basic problem with ampersands that does NOT involve URLs.

I have user data coming in from a post:

$comments = $_POST["comments"]

The data is "this is a &test"

No problem so far.

I need to take that data and put it into an Associate Array in order to send it out as part of a SOAP implementation.

The problem comes when someone types in an "&" into their data, such as "Bill & Sally".

The PHP code that creates the array crashes, viewing the ampersand as the PHP operator, not as text. URLencode and the other functions I've found so far are useless, because I can't use HRML or URL encoded format for the output (%26). It really needs to send the ampersand -as text-.

Here's the code that uses the $comments with the ampersand in it:

$fields = array (
'Ship_to_Company__c' => $shipcompany,
'Ship_To_Lead__c' => $leadid,
'Ship_to_Phone__c' =>$shipphone,
'Ship_to_Email__c' =>$shipemail,
'Shipto_Street__c' => $shipstreet,
'Shipto_City__c' => $shipcity,
'Shipto_State_Province__c' => $shipstate,
'Shipto_Zip_Postal_Code__c' => $shipzip,
'Shipto_Country__c' => $shipcountry,
'Ship_to_Other__c' => $shipname,
'Comments__c'=> $comments, .... etc.

And here's the error that produces (I believe it's a print_r in a try()):

The reference to entity "test" must end with the ';' delimiter.

It thinks it's an entity.

What is the workaround to use & in PHP code, not in HTML or URL output?
I tried escaping it with a backslash, and that didn't work.

Re: Problem with Ampersand (&) in array

Posted: Wed May 13, 2009 6:21 pm
by AAKAlan
I figured it out: htmlentities()

Sorry to waste your time. All is working well now.