prowebinteractive
asked on
strtok (php tokenizer)
from a text area I am entering emails seperating by a line break
eg:
123@123.com
456@456.com
321@123.com
etc etc.
the code below is inserting a space at the end of the email how could I stop that ?
what I have so far is:
//echo "bulk email database entry";
dbConnect();
$token = strtok($_POST['contents'], "\n -");
while($token)
{
echo $token . ";<br />";
$selectQuery = "SELECT email FROM emailTable WHERE email = '" . $token . "'";
$result = mysql_query($selectQuery)
or die ("Couldn't Insert New Email.");
$tokenRows = mysql_num_rows($result);
echo $tokenRows;
if($tokenRows == 0)
{
$insertQuery = "INSERT INTO emailTable (name, email)
VALUES('ENTERED BY ADMINISTRATOR', '" . $token . "')";
$insertResult = mysql_query($insertQuery)
or die ("Couldn't Insert New Email.");
}
$token = strtok("\n -");
dbDisconnect();
}
eg:
123@123.com
456@456.com
321@123.com
etc etc.
the code below is inserting a space at the end of the email how could I stop that ?
what I have so far is:
//echo "bulk email database entry";
dbConnect();
$token = strtok($_POST['contents'],
while($token)
{
echo $token . ";<br />";
$selectQuery = "SELECT email FROM emailTable WHERE email = '" . $token . "'";
$result = mysql_query($selectQuery)
or die ("Couldn't Insert New Email.");
$tokenRows = mysql_num_rows($result);
echo $tokenRows;
if($tokenRows == 0)
{
$insertQuery = "INSERT INTO emailTable (name, email)
VALUES('ENTERED BY ADMINISTRATOR', '" . $token . "')";
$insertResult = mysql_query($insertQuery)
or die ("Couldn't Insert New Email.");
}
$token = strtok("\n -");
dbDisconnect();
}
Hi, explode() is faster than preg_split()..
<?
//echo "bulk email database entry";
dbConnect();
$arr = explode("\n",$_POST['conte nts']);
foreach($arr as $token)
{
echo $token . ";<br />";
$selectQuery = "SELECT email FROM emailTable WHERE email = '" . $token . "'";
$result = mysql_query($selectQuery)
or die ("Couldn't Insert New Email.");
$tokenRows = mysql_num_rows($result);
echo $tokenRows;
if($tokenRows == 0)
{
$insertQuery = "INSERT INTO emailTable (name, email)
VALUES('ENTERED BY ADMINISTRATOR', '" . $token . "')";
$insertResult = mysql_query($insertQuery)
or die ("Couldn't Insert New Email.");
}
$token = strtok("\n -");
dbDisconnect();
}
?>
---
Harish
<?
//echo "bulk email database entry";
dbConnect();
$arr = explode("\n",$_POST['conte
foreach($arr as $token)
{
echo $token . ";<br />";
$selectQuery = "SELECT email FROM emailTable WHERE email = '" . $token . "'";
$result = mysql_query($selectQuery)
or die ("Couldn't Insert New Email.");
$tokenRows = mysql_num_rows($result);
echo $tokenRows;
if($tokenRows == 0)
{
$insertQuery = "INSERT INTO emailTable (name, email)
VALUES('ENTERED BY ADMINISTRATOR', '" . $token . "')";
$insertResult = mysql_query($insertQuery)
or die ("Couldn't Insert New Email.");
}
$token = strtok("\n -");
dbDisconnect();
}
?>
---
Harish
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> While explode is faster, you would have to add a str_replace for users inputting data from windows
I checked the code I posted in Windows :)
I checked the code I posted in Windows :)
ASKER
I suppose the $emails would be the POST variable?
I don't understand what you mean. It depends on how you get the value
$emails = preg_split("/\s*[\n\r]\s*/
And then use:
foreach($email as $token) {
}
-r-