Solved

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

Posted on 2008-06-24
7
521 Views
Last Modified: 2012-05-05
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
Comment
Question by:dnotestine
7 Comments
 
LVL 19

Accepted Solution

by:
LordOfPorts earned 200 total points
ID: 21860629
Try:

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

instead of:

$emails = array($_POST['EmailRecipients']);
0
 
LVL 8

Assisted Solution

by:MrEne
MrEne earned 50 total points
ID: 21860637

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

Open in new window

0
 
LVL 1

Assisted Solution

by:morristhebear
morristhebear earned 200 total points
ID: 21860644
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 19

Assisted Solution

by:bevhost
bevhost earned 50 total points
ID: 21860647
$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
 
LVL 1

Expert Comment

by:morristhebear
ID: 21860661
MrEne, yours works along the same line as mine but doesn't remove the whitespace as requested.
0
 
LVL 8

Expert Comment

by:MrEne
ID: 21860705
to remove spaces. hope that works.
$emails = explode ( ',' , $_POST['EmailRecipients'] );

$emails = str_replace(" ", "", $emails );

Open in new window

0
 

Author Comment

by:dnotestine
ID: 21861275
Everyone was so fast it was hard to distribute the points. Sorry if I upset anyone with the point spread.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 30
Preventing users manipulating URL strings 10 37
How to keep line breaks when outputting data from database 4 19
Checking if varaible is empty 6 32
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now