[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 = "";
}
}
?>
To use it, is very simple...
Code:
<?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>";
}
}
}
?>
Sorry for the weak english, i'm trying my best! uhauhauhuhaa
So, sugestions and criticals will be welcome!