08-29-2008, 12:07 PM
[eluser]sophistry[/eluser]
ok, got a few more tweaks here that should be a "final" version.
fixed the "capture dot at end of pattern problem"
made it much more strict overall... (see test cases in controller below).
EDIT: replaced a-z0-9 with [:alnum:] character class
please test and examine.
ok, got a few more tweaks here that should be a "final" version.
fixed the "capture dot at end of pattern problem"
made it much more strict overall... (see test cases in controller below).
EDIT: replaced a-z0-9 with [:alnum:] character class
Code:
<?php
class Test extends Controller {
function Test()
{
parent::Controller();
}
function index()
{
$chars = '-.a-zA-Z0-9#!$%&*+/\'=?^_`{}|~';
$len = strlen($chars);
$i=0;
print_r($chars); echo '<br>';
while ($i<$len)
{
preg_match(":[$chars]:", $chars[$i], $matches);
$i++;
print_r($matches[0]);
}
// test on "real" addresses
$strs = array(
"back|to=school~w0w.does+this^work?@sub.email.codeigniter.com",
"back{to}school-does+this^work@email.codeigniter.com",
'back{to}school#works!hostname@email.codeigniter.com',
'h.r@example.com',
'h.r@dot-at-end.example.com.',
'h.r@double-dot-at-end.example.com..',
'h.r@sub.example.com',
'h.r@sub-sub.email.example.com',
'h-r@example.com',
'.hr_does_not_capture_initial_dot@example.com',
'h#r@example.com',
'h!r@example.com',
'h$r@example.com',
'h%r@example.com',
'h&r;@example.com',
'h*r@example.com',
'h+r@example.com',
'h/r@example.com',
"h'r@example.com",
'h=r@example.com',
'h?r@example.com',
'h^r@example.com',
'h_r@example.com',
'h`r@example.com',
'h{r@example.com',
'h}r@example.com',
'h|r@example.com',
'h~r@example.com',
'h..r@example.com',
'how..r@example.com',
'how_wild..rare@example.com',
'h...r@example.com',
'h.r@example..com',
'h.r@tell.me.how.could.the.pattern.not.capture.this.example..com',
'h.r@-example-with-inital-dash.com',
'h.r@hostname-masks-erroneous-domain.-dash.com',
'h.r@dash-at-end-of-tld.com-',
'h.r@dash-at-end-of-domain-.com',
'h.r@.dot-in-front-of-domain.com',
);
$chars_not_dot = '-a-z0-9#!$%&*+/\'=?^_`{}|~';
foreach ($strs as $s)
{
preg_match_all(";([$chars_not_dot](?:[$chars_not_dot]|[.](?![.]))*)@((?:[[:alnum:]])(?:[-[:alnum:]]*)(?:[[:alnum:]])(?:[.](?![.]))(?:[[:alnum:]])(?:[-[:alnum:]]|[.](?![.]))+(?:[[:alnum:]]));i", $s, $matches);
$this->_p($s);
$this->_p($matches);
}
}
function _p($d) {echo'<pre>';print_r($d);echo'</pre>';}
}
/* End of file test.php */
please test and examine.