Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 538
  • Last Modified:

How to get a Web POST variable into an array using PHP

Using PHP 4.3.2 I'm wanting to validate email addresses that are entered into a form. There may be one or more adrresses but multiples will be separated by commas ','.
i.e - dbn@acme.com,sam@acme.com
The following code does not work. Any ideas?
Also how to trim spaces out of the entry in case of:
i.e - dbn@acme.com, sam@acme.com

$emails = array($_POST['EmailRecipients']);
for ($i = 0; $i < count($emails); $i++) {
      if (!eregi ("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", $emails[i])) {
            ReportAdminError("Email is incorrect - $emails[i]",'Error','report_template');
            exit;
      }
}

Thanks in advance,
David

0
dnotestine
Asked:
dnotestine
4 Solutions
 
LordOfPortsCommented:
Try:

$emails = split(",", $_POST['EmailRecipients']);
$emails = array_map("trim", $emails);

instead of:

$emails = array($_POST['EmailRecipients']);
0
 
MrEneCommented:

$emails = explode ( ',' , $_POST['EmailRecipients'] );

Open in new window

0
 
morristhebearCommented:
You can do this.


<?php
 
	//	Put all the emails into an array, first removing all the whitespace.
	$emails = explode (',', str_replace (' ', '', $_POST['EmailRecipients']));
	
	//	Test
	print_r ($emails);
	
?>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bevhostCommented:
$emails = explode(",", $_POST['EmailRecipients']);
for ($i = 0; $i < count($emails); $i++) {
      if (!eregi ("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", trim($emails[i]))) {
            ReportAdminError("Email is incorrect - $emails[i]",'Error','report_template');
            exit;
      }
}
0
 
morristhebearCommented:
MrEne, yours works along the same line as mine but doesn't remove the whitespace as requested.
0
 
MrEneCommented:
to remove spaces. hope that works.
$emails = explode ( ',' , $_POST['EmailRecipients'] );
$emails = str_replace(" ", "", $emails );

Open in new window

0
 
dnotestineAuthor Commented:
Everyone was so fast it was hard to distribute the points. Sorry if I upset anyone with the point spread.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now