+ Start a Discussion
VornoVorno 

ampersand (&) in Account Name

Hey,

Having a problem updating an Account Name where an ampersand is present.

PHP is returning the following error:
Uncaught SoapFault exception: [soapenv:Client] The entity name must immediately follow the '&' in the entity reference.

Now I've tried escaping the ampersand (eg: \&) to no avail... Is there a way to use this character with PHP / Salesforce API ? Can I use Unicode or HTML encoding on this?
Best Answer chosen by Admin (Salesforce Developers) 
JerryJosephJerryJoseph
fixing ampersands alone wont solve issues.. best practice is to use htmlspecialchars for all datatypes of type string.. this can avoid issues regarding characters like & < > etc..

$str = htmlspecialchars($str);

All Answers

SuperfellSuperfell
Sounds like the PHP toolkit is not correctly escaping the & to &amp;
VornoVorno
Thanks for your help Simon, this has fixed it.
slugmandrewslugmandrew
Can anyone provide info on how to fix this? 

I am having the same problem but can't find any info on what needs to be changed. 

I'm using the sample login.php that comes with the php developer toolkit 1.1, it works fine to edit account unless they have an ampersand in the company name, in which case I get this error:

There was a problem with your login: The entity name must immediately follow the '&' in the entity reference.

If anyone can help i'd appreciate it.
VornoVorno
You need to replace any and every ampersand (&) with it's HTML equivilent:

I use a php function I wrote:
Code:
function fixamp($str)
{
 $str = str_replace("&", "&amp;", $str); // str_replace(find, replace, haystack);
 return $str;
}

 then when I'm adding fields to a new sObject:

            $oppfields["Name"]         = fixamp("$accname - $month $year");
(as an example).

slugmandrewslugmandrew
Thanks for your help, vorno.

I was thinking about using a script to do this but it seemed a little long-winded for such a simple thing.  I got some advice from Mike Simonds and just changed this line:

Code:
$name = $_POST['AccountName'];

to this:

Code:
$name = htmlspecialchars($_POST['AccountName']); 

Check out his salesforce tutorials if you haven't come across his site already: www.mikesimonds.com

Now it works fine, but I'm confused as to why it didn't work in the first place because I didn't change anything in the toolkit and it doesn't seem like that many people have had this problem.  Any ideas?

<slugmandrew>
JerryJosephJerryJoseph
fixing ampersands alone wont solve issues.. best practice is to use htmlspecialchars for all datatypes of type string.. this can avoid issues regarding characters like & < > etc..

$str = htmlspecialchars($str);
This was selected as the best answer
Aaron BaumanAaron Bauman
I know this is an old thread, but I have found htmlspecialchars to be insufficient in addressing extended ascii, unicode, and other characters. Has anyone run into such an issue?
twongopenairtwongopenair

In another thread, html encoding to hex instead of the entity name worked for me with extended characters and unicode.