Jump to content

[SOLVED] If inside foreach?


papaface

Recommended Posts

Hello,

I have the following code:

<?php

$filename = "email_send.txt";
$lines = file($filename);

foreach($lines as $line_num => $line)
{
if	(ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$line))
	{
	//mail('$line', 'ORDER', $msg, "Content-Type: text/html\nFrom: [email protected]");
                  echo $line;
	}
}

?>

When i run this, it only prints one email address out when there are three valid emails in the text file.

i have found that it is because if the if statement that this occurring, how do I get around this problem.

I want something to happen to the email address on each line of the text file IF the email is valid using the regex

Link to comment
https://forums.phpfreaks.com/topic/54655-solved-if-inside-foreach/
Share on other sites

fixed

<?php

$filename = "emails.txt";
$lines = file($filename);


foreach($lines as $line)
{
$line = rtrim($line);
if	(ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$line))
	{
	//mail($line, 'ORDER', test, "Content-Type: text/html\nFrom: [email protected]");
	echo $line ."<Br>";
	}
}

?>

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.