Page 1 of 1

entry format manipulation

Posted: Sat Apr 30, 2005 3:47 am
by orangeapple
Hi there,

I have a problem with a form where my customer can enter their email adress. They are allowed to enter several email addresses, for example :

y.mollard@microsent.com, m.blanchet@microsent.com, jp.chapotel@microsent.com

this works fine, but if they enter :

y.mollard@microsent.com,m.blanchet@microsent.com,jp.chapotel@microsent.com

without a space after the coma, it modifies the layout of my customer details display page because it does display all the addresses on one line.

How can I control that the customers put a space after each coma ?

Thanks a lot for your help.

Posted: Sat Apr 30, 2005 8:35 am
by Chris Corbyn
It's difficult to control user input. the best thing to do would be to channge the way you separate the email addresses when the data hits the server.

I'f you're just using explode() or substr() then you'll have to have several conditions for what you predict a user may input (and that will fails of they are inconsistent with their inputs i.e. joe.bloggs@email.com, will.smith@domain.co.uk ,kerrey.jonson@foo.bar.....)

I'd suggest using a regular expresion (preg_split()) to spearate the addresses.

Posted: Sat Apr 30, 2005 9:01 am
by John Cartwright
preg_* is probably a better way, but a dirty way would be to check if there is a space, remove it then insert them all the same in the database. Then explode your email addresses and display them appropriatly.

Code: Select all

function fix_email($old_string)
{
     $new_string = str_replace(' ','',$old_string);
     //supress errors if there are no commas present
     $new_string = @explode(',',$new_string);
     $count = count($new_string);

     if ($count > 0)
     {
          for($x = 0; $x >= $count; $x++;)
          {
               $fixed_string .= $new_string[$x] . ($count == $x ? '' : ', ');
          }
     }
     else
     {
          $fixed_string = $old_string;
     }

     return $fixed_string;
}
That should give you a bit of help