Link to home
Start Free TrialLog in
Avatar of prowebinteractive
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();      
            }
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Can't you use:

$emails = preg_split("/\s*[\n\r]\s*/");

And then use:

foreach($email as $token) {
}

-r-
Hi, explode() is faster than preg_split()..

<?
//echo "bulk email database entry";
dbConnect();
$arr = explode("\n",$_POST['contents']);
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
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> 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 :)
Avatar of prowebinteractive
prowebinteractive

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