Jump to content

[SOLVED] PHP Newbie


Jellz

Recommended Posts

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>

Link to comment
Share on other sites

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!";
}

Link to comment
Share on other sites

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);
}
}
?>
    

 

Link to comment
Share on other sites

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);
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.