Forum Moderators: coopster

Message Too Old, No Replies

Lost accents with form

         

Stenness

11:49 am on Jun 22, 2006 (gmt 0)

10+ Year Member



Dear friends,
I'm using a php script called apgForm 1.7 (that generates an e-mail message and updates an excel file containing the form data) and I'm having problems with accented letters. Any word with accents is getting scrambled, I think because of a wrong text encoding.
For instance, if I put a fake name like this:

àèéìòùç

The result on the e-mail message and the excel file is:

‡ËÈÏÚ?Á

I wonder how I could make the script correctly recognize those accented French letters.

Below is the php code of the script I'm working on:


<?php
/*
apgForm 1.7
By: Alvaro Prieto (apg88)
E-Mail: webmaster@apg88.com
Web site: [apg88.com...]

Licensed under the Creative Commons Attribution 2.5 License - [creativecommons.org...]
[creativecommons.org...] (Summary)
You can modify the code all you want, make money with it, release newer versions, etc... just leave my name, email, and web site URL on the it.

*/

// Change this to the page you want the user to be redirected to after form is submitted (i.e. success.html and error.html)
$success = "http://xyz.fr/success.html";
$error = "http://xyz.fr/error.html";
$mailto = 'xyz@xyz.com'; $mailsubject= 'Cordonnees Partenaire';

// Change this to the character(s) you want to be placed instead of line breaks(new line, enter, etc)
$lbChar = " ";// default is a space, you may change it to whatever you want

// Don't change anything below this line

// Determine if the form was sent through the GET methog or the POST method.
if($_POST){
$array = $_POST;
} else if($_GET){
$array = $_GET;
} else {
die("You must Access this file through a form.");// If someone accesses the file directly, it wont work :)
}

$denied=0;
if(!($array['Nom'])) {
echo "S'il vous pla&icirc;t, renseignez le champ Nom<br>";
$denied = 1;
}
if(!($array['Prenom'])) {
echo "S'il vous pla&icirc;t, renseignez le champ Pr&eacute;nom<br>";
$denied = 1;
}
if(!($array['Societe\''])) {
echo "S'il vous pla&icirc;t, renseignez le champ Soci&eacute;t&eacute;<br>";
$denied = 1;
}
if(!($array['Secteur'])) {
echo "S'il vous pla&icirc;t, renseignez le champ Secteur<br>";
$denied = 1;
}
if(!($array['Adresse'])) {
echo "S'il vous pla&icirc;t, renseignez le champ Adresse<br>";
$denied = 1;
}
if(!($array['CP'])) {
echo "S'il vous pla&icirc;t, renseignez le champ CP<br>";
$denied = 1;
}
if(!($array['Ville'])) {
echo "S'il vous pla&icirc;t, renseignez le champ Ville<br>";
$denied = 1;
}
if(!($array['Telephone'])) {
echo "S'il vous pla&icirc;t, renseignez le champ T&eacute;l&eacute;phone<br>";
$denied = 1;
}
if(!($array['Email'])) {
echo "S'il vous pla&icirc;t, renseignez le champ Email<br>";
$denied = 1;
}
if($denied){
die("Le formulaire N'A PAS &Eacute;T&Eacute; ENVOY&Eacute;");
}

//Check if the filename was sent through the form or not
if(!$array['filename']){
// if the filename wasnt sent through the form, it will become form.xls, you can change the default if you want.
$array['filename'] = "integrateurs.xls";//Set the file to save the information in

} else {
if(!(stristr($array['filename'],".xls"))){
$array['filename'] = $array['filename'] . ".xls";
}
}

// Define the tab and carriage return characters:
$tab = "\t";//chr(9);
$cr = "\n";//chr(13);

if($array){
// Make The Top row for the excel file and store it in the $header variable
$keys = array_keys($array);
foreach($keys as $key){
if(strtolower($key)!= 'filename' && strtolower($key)!= 'title'){
$header .= $key . $tab;
}
}
$header .= $cr;

//Make the line with the contents to write to the excel file.
foreach($keys as $key){
if(strtolower($key)!= 'filename' && strtolower($key)!= 'title'){

$array[$key] = str_replace("\n",$lbChar,$array[$key]);
$array[$key] = preg_replace('/([\r\n])/e',"ord('$1')==10?'':''",$array[$key]);
$array[$key] = str_replace("\\","",$array[$key]);
$array[$key] = str_replace($tab, " ", $array[$key]);
$data .= $array[$key] . $tab ;
$email_data .= $key . " : " . $array[$key] . "\n";
}
}
$data .= $cr;

if (file_exists($array['filename'])) {
$final_data = $data;// If the file does exist, then only write the information the user sent
} else {
$final_data = $header . $data;// If file does not exist, write the header(first line in excel with titles) to the file
}
// open the file and write to it

$fpem = mail($mailto, $mailsubject, $email_data . "\nLien au fichier Excel: [xyz.fr...]

$fp = fopen($array['filename'],"a"); // $fp is now the file pointer to file $array['filename']

if($fp){

fwrite($fp,$final_data);//Write information to the file
fclose($fp);// Close the file
// Success
header("Location: $success");
} else {
// Error
header("Location: $error");
}
}
?>


Any help would be greatly appreciated!

Scally_Ally

12:09 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



could you not strip these characters using the htmlentities() function?

Failing that you could find the ascii code of each character and strip/replace them that way using the str_replace() function.

Ally

Stenness

12:46 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



I'm not a PHP expert, so can you please be more specific, describing exactly how can I add the functions you suggested to the script above?

Thanks a million

Scally_Ally

10:42 am on Jun 23, 2006 (gmt 0)

10+ Year Member



Im kinda guessing cause i dont know exactly what the script is doing but..

In this section

//Make the line with the contents to write to the excel file.
foreach($keys as $key){
if(strtolower($key)!= 'filename' && strtolower($key)!= 'title'){

$array[$key] = str_replace("\n",$lbChar,$array[$key]);
$array[$key] = preg_replace('/([\r\n])/e',"ord('$1')==10?'':''",$array[$key]);
$array[$key] = str_replace("\\","",$array[$key]);
$array[$key] = str_replace($tab, " ", $array[$key]);
$data .= $array[$key] . $tab ;
$email_data .= $key . " : " . $array[$key] . "\n";
}
}
$data .= $cr;

change to something like

//Make the line with the contents to write to the excel file.
foreach($keys as $key){
if(strtolower($key)!= 'filename' && strtolower($key)!= 'title'){

$array[$key] = str_replace("\n",$lbChar,$array[$key]);
$array[$key] = preg_replace('/([\r\n])/e',"ord('$1')==10?'':''",$array[$key]);
$array[$key] = str_replace("\\","",$array[$key]);
$array[$key] = str_replace($tab, " ", $array[$key]);
$array[$key] = htmlentities($array[$key]);
$data .= $array[$key] . $tab ;
$email_data .= $key . " : " . $array[$key] . "\n";
}
}
$data .= $cr;

adding this line
$array[$key] = htmlentities($array[$key]);
converts characters in a string that are html characters. eg & changes to &amp;

if this doesnt work use another line of str_replace() to get rid of characters using their ascii code.
eg str_replace(myString, chr(13), "e`");
this second option is a bit crued but gives you more options.

Ally