Jellz Posted August 21, 2009 Share Posted August 21, 2009 Im fairly new to PHP, been reading for some time and copying things from books, but am just now trying to develop my own app. I'm looking to take a line from my mySql database, see if there is an email address, and if so to save that. Some lines may have extra stuff in them like "hello bob john@hisdomain.com how are you". My thought was to store each row as a string, then to explode that string into an array based off the " " (space). Then to test to see if a member of an array had the "@", if so to store that into another array, if not, to spit out "No Email address found". Here is where I'm at. Any suggestions would be appreciated. <html> <head> <title>Data Manipulation</title> <?php include 'header.php'; ?> </head> <body> <h2>Email Address Extract</h2> <?php //convert email column to string $result = mysql_query("SELECT email FROM users"); //array to store strings with @ $please=array(); while($row = mysql_fetch_array($result)) { $address = (string)$row['email']; $findme = '@'; $test1 =(explode(" ",$address)); $pos = strpos($test1, $findme); if($pos === false) { echo "No email address found<br />"; } else { array_push($please,"$test1"); } print_r ($please); } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/171356-solved-php-newbie/ Share on other sites More sharing options...
dreamwest Posted August 22, 2009 Share Posted August 22, 2009 My thought was to store each row as a string, then to explode that string into an array based off the " " (space). Then to test to see if a member of an array had the "@", if so to store that into another array, if not, to spit out "No Email address found". $string = "hello bob johnhis@domain.com how are you"; $find = explode('@', $string); if($find['1'] != ""){ echo "I have a @ in my string!"; }else{ echo "Doesnt have @ in string!"; } Quote Link to comment https://forums.phpfreaks.com/topic/171356-solved-php-newbie/#findComment-903715 Share on other sites More sharing options...
trq Posted August 22, 2009 Share Posted August 22, 2009 Not sure it helps but you could simply narrow down your query. SELECT email FROM users WHERE email LIKE '%@%'; Quote Link to comment https://forums.phpfreaks.com/topic/171356-solved-php-newbie/#findComment-903716 Share on other sites More sharing options...
Jellz Posted August 22, 2009 Author Share Posted August 22, 2009 Thanks for the suggestions but neither of those ended up doing quite what I'm looking for. I've made some modifications and think I am fairly close so I'll get really specific with where I'm at and see if anyone has any helpful suggestions. Here is the PHP piece of my code. I use the include function to store the connection to my database so just assume that's done correctly (b/c it is). I think the real problem is if I am using the foreach function properly, as well as if attempting to search for the "@" symbol using the == operator. My comments are highlighted in orange so you can at least see my thought process. I've been plugging at this for hours and just can't QUITE get it. Ideas appreciated. <?php //convert email column to string $result = mysql_query("SELECT email FROM users"); //create please array to store valid email addresses $please=array(); $findme = "@"; //for each row of 'email' in table 'users' while($row = mysql_fetch_array($result)) { $address = (string)$row['email']; //take the entire string and explode each word into array $words using the ' ' as a delimiter $words=array(explode(' ', $address)); //loop over $words applying its value to $value foreach($words as $value) { //if the $value has $findme (the @) if($value == "$findme"){ //echo $value and push it to $please array echo "I have a @ in my string! <br />"; array_push($please, "$value); }else{ echo "Doesnt have @ in string! <br />" unset($value); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171356-solved-php-newbie/#findComment-903931 Share on other sites More sharing options...
Jellz Posted August 22, 2009 Author Share Posted August 22, 2009 Finally got it. Thanks for all the suggestions <?php $result = mysql_query("SELECT email FROM users"); $valid_Emails=array(); $findme = '@'; while($row = mysql_fetch_array($result)) { $address = (string)$row['email']; $words=array(explode(' ', $address)); foreach ($words as $value) { $numofrows = count($value); for ($i = 0; $i < $numofrows; $i++) { if(strpos($value[$i],$findme) === false) { } else { echo "@ was found in " . $value[$i] . "<br />"; array_push($valid_Emails, $value[$i]); } } } } $csv_file = implode(",", $valid_Emails); ?> Quote Link to comment https://forums.phpfreaks.com/topic/171356-solved-php-newbie/#findComment-904062 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.