Jump to content

Check variables in Foreach


christa

Recommended Posts

Because input from a form is always a string so you cannot use is_int(). Use is_numeric() or ereg. Bricktop: nothing to do with trimming however good idea to trim each array element.

 

if(ereg('[0-9]+', $number, $regs)) {
  print "Numeric";
}

ok.

How can I continue only if all the values that I enter in textarea are matched by erg()?

 

Example:

 

if i enter:

123

456

qwerty

789

 

the code goes on and print OK.

 

Instead, i must go on only if all values entered are matched by ereg (= only numbers 0-9)

I would log the invalid entries

<?php
$recipient = explode("\n", $_POST['recipient']);
if(is_array($recipient)) {
$results = array();
foreach($recipient as $number) {
	$number = trim($number);
	if(ereg('[0-9]+', $number, $regs)) {
		// number is valid
		$results['valid'][] = $number;
	} 
	else {
		// entry is invalid
		$results['invalid'][] = $number;
	}
}
// check for invalid entries
if(count($results['invalid'])) {
	print "The following entries are invalid:<br />".implode("<br />",$results['invalid'])."<br />";
}
// display valid entries
if(count($results['valid'])) {
	print "The following entries are valid:<br />".implode("<br />",$results['valid'])."<br />";
}
}
else {
print "Missing input";
exit();
}
?>

sorry if i reopen this topic.

I've seen an issue in the code of neil.johnson

 

If i write something as this:

 

123

546

456

esr

564

opo

 

the code always will print :

those entries are invalid:
esr
opo
those entries are valid:
123
546
456
564

 

There is a way to print like this one?

Valid entries: 4
Invalid entries: 2

and viceversa

 

 

 

 

<?php
print "Valid Entries ".count($results['valid'])."<br />";
print "Invalid Entries: ".count($results['invalid']);
?>

in this way, it will always print

Valid Entries: 6
Invalid Entries: 6

 

because it check always all entries in the textarea:

1 esr

2 opo

3 123

4 546

5 456

6 564

This is 100% correct. You must have ammended it somewhere.

<?php
$recipient = explode("\n", $_POST['recipient']);
if(is_array($recipient)) {
$results = array();
foreach($recipient as $number) {
	$number = trim($number);
	if(ereg('[0-9]+', $number, $regs)) {
		// number is valid
		$results['valid'][] = $number;
	} 
	else {
		// entry is invalid
		$results['invalid'][] = $number;
	}
}
// check for invalid entries
if(count($results['invalid'])) {
	print "Invalid Entries: ".count($results['invalid'])."<br />";
	//print "The following entries are invalid:<br />".implode("<br />",$results['invalid'])."<br />";
}
// display valid entries
if(count($results['valid'])) {
	print "Valid Entries: ".count($results['valid']);
	//print "The following entries are valid:<br />".implode("<br />",$results['valid'])."<br />";
}
}
else {
print "Missing input";
exit();
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.