Welcome Guest, Not a member yet? Register   Sign In
EmailDetect Library - A simple solution to detect emails into strings.
#1

[eluser]Joel Wallis[/eluser]
Hi CI Masters!
I'm here to share a library that i write to find valid emails into string datas. It's a simple (really simple!) library, that you can use to work with various emails at the same time.

So... Let's to the code.

Code:
<?php

/**
* EmailDetect
*
* Provide methods to crawling some string and find valid emails.
*
* @author    Joel Wallis <[email protected]>
* @version    1.0
*
* @access    public
* @param    string    $str    The string that contain or not valid emails.
* @return    void
*/

class EmailDetect {
    
    private        $ci;
    public        $input;
    public        $list = array();
    public        $qtd;
    private        $regex = '/[^\da-zA-Z@\.-]/mi';
    
    
    public function __construct($str = null)
    {
        $this->ci =& get_instance();
        $this->ci->load->helper('email');
        
        if($str)
        {
            $this->input = $str;
            $this->run();
        }
    }
    
    
    /**
     * run
     * void run() - Run the crawling.
     *
     * @access    private
     * @return    void
     */
    
    public function run($str = null)
    {
        $this->input .= $str;
        $this->split();
    }
    
    
    /**
     * split
     * void split() - Split the string with any that isn't a letter, a number or the '@', '_' and '.' chars.
     *
     * @access    private
     * @return    void
     */
    
    private function split()
    {
        $this->input    = strtolower($this->input);
        $input_list        = preg_split($this->regex, $this->input);
        
        foreach($input_list as $item)
        {
            if(valid_email($item) && count(array_keys($this->list, $item)) == 0)
            {
                $this->list[] = $item;
            }
        }
        
        $this->qtd = count($this->list);
        $this->input = "";
    }
}

?&gt;


To use it, is very simple...

Code:
&lt;?php
class MyController extends Controller
{
    function index()
    {
        $this->load->library('emaildetect');
        $this->emaildetect->run($this->input->post('textarea_emails'));
        
        /*
            You can take your emails into the list array property ($this->emaildetect->list).
            You can load the library and inserting data to the crawling at same time.
        */
        
        $this->load->library('emaildetect', $this->input->post('textarea_emails'));
        
        /*
            For example, you can print the emails...
        */
        
        print "<h2> Printting the emails ! </h2>";
        
        foreach($this->emaildetect->list as $id => $email)
        {
            print "<p> list[$id] : $email </p>";
        }
    }
}
?&gt;

Tongue

Sorry for the weak english, i'm trying my best! uhauhauhuhaa
So, sugestions and criticals will be welcome!
#2

[eluser]stensi[/eluser]
That's pretty cool. Definitely useful for finding emails in free text fields where it's in the site owner's interest to obfuscate them in some way (ROT13), to prevent bots from spamming their users!

I'll test it out when I get the chance, and then will likely use it for the above mentioned purpose.
#3

[eluser]Jamie Rumbelow[/eluser]
Nice!

Pretty useful too. My only concern is that a simple explode() and valid_email() will suffice... the overheads of a library might take it's toll in a production environment.
#4

[eluser]gungbao[/eluser]
i would rather find it more efficient to use a regex for that purpose, regex is made for exactly such tasks (and billions more Wink
simply google for it and you will not need to split+iterate
cheers
chris
#5

[eluser]Armorfist[/eluser]
Many thanks!
Believe it or not, I was doing one myself, and the regex wasn't going very well at all. You just saved me a lot of time!
#6

[eluser]Armorfist[/eluser]
Hello,

I just found a regex that does this very simplified:

Code:
//String
$string = "This is some text with [email protected] in it. You must strtolower this string so the regex can work [email protected] ?";
//Exec Regex
preg_match_all('^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}^',strtolower($string),$regs);
//Print result
echo "<pre>";
var_dump($regs);
echo "</pre>";

Hope it helps!
#7

[eluser]gungbao[/eluser]
ahhhm, you do not need any strtolower, you should use the /i in the preg-regex
you should use the php "eregi", thats very much faster than the perl-compatible regex-engine
#8

[eluser]Armorfist[/eluser]
Thanks for the tips gungbao!
#9

[eluser]gungbao[/eluser]
would be glad if I could help.
well, I am not a regex-guru and without my nice oreilly book, i do have only the basics on mind.
but

'^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}^'

has a syntax.error (the final ^) and should better try the modified one

'[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}'

the eregi() should grep out all matched emails in the $regs-array
http://www.php.net/manual/de/function.eregi.php

have fun with regular expression, they are extremely powerful (e.g. see backtracking features. & more)
cheers,
chris
BTW - you homepage is nicely designed
#10

[eluser]Armorfist[/eluser]
[quote author="gungbao" date="1221190956"]would be glad if I could help.
well, I am not a regex-guru and without my nice oreilly book, i do have only the basics on mind.
but

'^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}^'

has a syntax.error (the final ^) and should better try the modified one

'[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}'

the eregi() should grep out all matched emails in the $regs-array
http://www.php.net/manual/de/function.eregi.php

have fun with regular expression, they are extremely powerful (e.g. see backtracking features. & more)
[/quote]
I'm just starting to learn regex, would it really help to buy a book? You mentioned the O'reilly one, I think I'll give it a try.
[quote author="gungbao" date="1221190956"]
BTW - you homepage is nicely designed
[/quote]
Thanks Wink




Theme © iAndrew 2016 - Forum software by © MyBB