2011-01-16 7 views
1

#a%$$$ [email protected]#$#$2344324.comの電子メールを[email protected]にサニタイズします。
私は試して失敗しました
echo filter_var( "a#$%#$ @ [email protected]#$#$2344324.com"、FILTER_SANITIZE_EMAIL); //結果:a#$%#$ @ [email protected]#$#$2344324.compreg-replaceを使用して電子メールの不要な文字を削除する

電子メールで特殊文字をトリミングする必要があります(特殊文字を削除するにはサニタイズします)。私は以下のコードを使用しましたが、失敗しました。

$string = preg_replace("/^[a-zA-Z0-9._%+-][email protected](?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/", "", "a#$%#[email protected]@#$#$2344324.com"); 

echo $string;//result: [email protected] -- unwanted characters trimmed here. 
+0

試してみて、それをやろうとしているか、あなたが、やろうとしているかを説明する、あなたが見ることを期待するものは、なぜあなたが見たものは、その期待を満たしていませんでした。このサイトには実際の専門家がいますが、通常はその専門知識はソフトウェアにありますが、それは利便性ではありません – davin

+1

そのような行動には意味がありません –

+2

'#$%#$ @ [email protected]#$#$2344324.com 'を' a @ b.com'に変換しますか? – Gumbo

答えて

0

ここではRFCベースのソリューションすでにあります:http://fightingforalostcause.net/misc/2006/compare-email-regex.php

function is_valid_email_address($email){ 

     $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'; 

     $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'; 

     $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'. 
      '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'; 

     $quoted_pair = '\\x5c[\\x00-\\x7f]'; 

     $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d"; 

     $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22"; 

     $domain_ref = $atom; 

     $sub_domain = "($domain_ref|$domain_literal)"; 

     $word = "($atom|$quoted_string)"; 

     $domain = "$sub_domain(\\x2e$sub_domain)*"; 

     $local_part = "$word(\\x2e$word)*"; 

     $addr_spec = "$local_part\\x40$domain"; 

     return preg_match("!^$addr_spec$!", $email) ? true : false; 
    } 
関連する問題