Hello,
I have a script that takes a block of text and goes through it looking to see if any of the words is an email address, if so, the code will format the email address as such.
Everything works except for the emaila ddress validation. It always returns false. I initially wrote my own. that did not work, currently i am on try 5 with code I got from other php sites. None work - they all return false.
Even if I try to pass only an emaila ddress to these functions, they return false.
Here is the code (checkEmail()) is the validation function:
<?
/**
* Checks if the string provided is an email
* return boolean
**/
function checkEmail($word) {
$result = TRUE;
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
$result = FALSE;
}
return $result;
}
/**
* Gets the word and makes sure there are no trailing characters
* return string
**/
function processWord($word)
{
/* start checking for special character endings */
$newWord = $word;
$wordLength = strlen($word);
$specialEndCharBoo = false;
$endChar = $word{$wordLength-1};
if($endChar == "." || $endChar == "," || $endChar == "?" || $endChar == ";" || $endChar == "!")
{
$specialEndCharBoo = true;
$newWord = substr($word, 0, ($wordLength-1));
}
/* End looking for last character */
echo $newWord."<br />";
if(checkEmail($newWord))
{
$returnWord = "<a href='mailto:".$newWord."'>".$newWord."</a>";
}
else
{
$returnWord = $newWord;
}
if($specialEndCharBoo)
{
$returnWord = $returnWord.$endChar;
}
return $newWord;
}
/**
* Splits a bock of text into individual words
* return string
**/
function getEmailorURL($block)
{
$words = explode(" ", $block);
$length = sizeof($words);
$newblock = "";
$space = "";
for($i = 0; $i < $length; $i++)
{
$returnWord = processWord($words[$i]);
$newblock = $newblock.$returnWord." ";
}
return $newblock;
}
$block = "Please send resumes to true@email.com. true@email.com is what the email is, baby.";
$newblock = getEmailorUrl($block);
echo $newblock;
?>
Thanks in advance.