regex - How can I catch the following obfuscated email addresses in PHP? -
Consider the following script, which contains ambiguous email addresses, and a function based on *** Tries to change **
using regex pattern matching. My script tries to capture the word: "at", "at", "at", "@"
after some text (a domain name), "dot" " After. "" Dot "
, after a TLD.
Input:
$ str [] = 'dsfatasdfasdf asd dsfasdf dsfdsf @ hotmail.com'; $ Str [] = 'I'm at school where I know dsfdsf@hotmail.com'; $ Str [] = 'I live in school my address is dsfdsf@hotmail.com'; $ Str [] = 'My address at school is dsfdsf@hotmail.com'; $ Str [] = 'dsf a t asdfasdf asd dsfasdf dsfdsf@hotmail.com'; $ Str [] = 'DSFDSF A T HOTM One I LCOM'; Function clean_text ($ text) {$ pattern = '/ (\ ba [\. -_] * t \ b | @) [\. \ -_] * (. +) [\. \ -_] * (d [\. \ -_] * o [\ -_] * t. \) [\. \ -_] * (c [\. \ -_] * o [\. \ -_] * M | n [\. -_] * E [\. \ -_] * TOO [\. \ - _] * R [\. -_] * g | ([az] [\. \ -_] *) {2,3} [az]) / IU '? Return preg_replace ($ pattern, '***', $ text); } Foreach ($ str $ as email) {cleano_text echo ($ email); }
Expected output:
dsfatasdfasdf asd dsfasdf dsfdsf *** I am at school where my address is dsfdsf @ * ** I live in school my address dsfdsf @ *** *** dsf *** dsfdsf ***
Result:
< Pre> dsfatasdfasdf AD dsfasdf dsfdsf *** I live *** I live in school *** My address is dsfdsf **** dsf *** dsfdfs ***
Problem: This grabs the first occurrence of "first", and not the last, so is the following:
Inp Q: 'My address in school produces dsfdsf@hotmail.com': Production of '****' should be: 'My address in school is dsfd ****'
< / Pre>How can I fix this?
Dsfdsf@hotmail.com ',' I am in the school where I know dsfdsf@hotmail.com ',' I live in school. My address is dsfdsf@hotmail.com, 'My address in school is dsfdsf@hotmail.com asdfasdf asd dsfasdf dsfdsf @ h at dsf,', 'otmail.com', 'dsf dsf a t h ot M an i l COM '); Forex Currency ($ Email $ as Email) {$ found = preg_match ('/(.*?) ((\\' | one [_. -] * t) [\ w .-] *? $) / ', $ Email, $ matches); If ($ found) {echo 'username:'. $ Matches [1] ', domain:'. $ Matches [2] "\ N"; }}
Output:
Username: dsfatasdfasdf asd dsfasdf dsfdsf, domain: @ hotmail.com Username: where I live in school my address Dsfdsf is, domain: @ hotmail.com Username: I live in school, my address is dsfdsf, domain: @ hotmax. Username: My address in school is DSFDF, Domain: @ Hotmax. Username: DSF AT asdfasdf asd dsfasdf dsfdsf, Domain: @ Hotmail. Username: DSFDSF, Domain: ATHOTM AIL COM
Comments
Post a Comment