entry format manipulation

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
User avatar
orangeapple
Forum Commoner
Posts: 70
Joined: Tue Jan 06, 2004 1:24 pm
Location: Geneva / Switzerland

entry format manipulation

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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