2013-03-16 1 views
0

저는 PHP에 익숙해 져서 불쌍한 지식을 변명하지만 사용자가 내 웹 페이지에 등록 할 때 전자 메일 양식을 사용하고 있지만 오류가 발생합니다. 나는 그것을 교체를 인터넷 검색 후eregi를 preg_match로 대체하면 오류가 발생합니다.

 $error_message = ""; 
    $email_exp = "^[A-Z0-9._%-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$"; 
    if(!eregi($email_exp,$email_from)) { 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 

:

<?php 
if(isset($_POST['email'])) { 

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to = "[email protected]"; 
    $email_subject = "New e-mail subscriber"; 


    function died($error) { 
     // your error code can go here 
     echo "We are very sorry, but there were error(s) found with the form your submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation expected data exists 
    if 
     (!isset($_POST['email'])) { 
     died('We are sorry, but there appears to be a problem with the email your submitted.');  
    } 


    $email_from = $_POST['email']; // required 

    $error_message = ""; 
    $email_exp = "/^[A-Z0-9._%-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/"; 
    if(!preg_match($email_exp,$email_from)) { 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 

    if(strlen($error_message) > 0) { 
    died($error_message); 
    } 
    $email_message = "Form details below.\n\n"; 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 


    $email_message .= "Email: ".clean_string($email_from)."\n"; 


// create email headers 
$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers); 
?> 

<!-- include your own success html here --> 

Thank you for contacting us. We will be in touch with you very soon. 

<? 
} 
?> 

먼저 나는 때문에 다음과 같이이었다 초기 스크립트의 오류 "eregi는 감가 상각"했다 :

전체 스크립트는 다음과 같다 다음을 포함합니다 :

$error_message = ""; 
    $email_exp = "/^[A-Z0-9._%-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/"; 
    if(!preg_match($email_exp,$email_from)) { 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 

그러나 이제 이메일 주소는 다음과 같습니다. 올바른 :

죄송합니다. 이 제출 된 양식에 오류가있었습니다. 이러한 오류는 아래에 나와 있습니다.

입력하신 이메일 주소가 유효하지 않습니다.

돌아가서 이러한 오류를 수정하십시오.

도움이 많이

+0

조언 : 웹에서 둘 이상의 (전자 메일) 양식을 찾아서 테스트하십시오. 하나는 실패없이 일할 의무가있다. 문자 그대로 이미 수천 가지가 있습니다. 이미 많은 보안 기능과 유효성 검사 기능이 내장되어 있습니다. 너 자신에게만 국한하지 마라. 건배! –

답변

1

eregi하는 경우를 구분 apreciated됩니다 preg_match는 없습니다.

패턴 끝에 (/..../i) i을 추가해야합니다.

이제
$email = filter_var($email_from, FILTER_VALIDATE_EMAIL); 

$email 필터링 된 전자를 포함합니다 : 유효한 이메일 주소를 확인하려면

+0

대단히 감사합니다! – David

0

아니 직접적인 대답은, 그러나, PHP의 내장 filters 사용하는 것이 훨씬 더 쉽다 - 메일 주소 또는 false (이 경우 유효한 주소가 아님).

-1

"." 만약 당신이 그것을하지 않으면 모든 문자와 일치 할 것이기 때문에 [] 안의 문자는 다음과 같습니다 :

"/^[A-Z0-9\._%-][email protected][A-Z0-9\.-]+\.[A-Z]{2,4}$/i" 
+0

잘못되었습니다. ^, \, - 및] 메타 문자 만 [] 안에서 특별한 의미를 가지며 이스케이프해야합니다 (^ 처음의 경우에만 - 마지막이 아닌 경우에만). –

+0

@ Phil Ok. 내 Regex가 귀하의 의견과 관련하여 유효하지 않습니까? –

+0

특정 진술을 "." 그것이 모든 것과 일치 할 것이기 때문에 탈출해야한다. 그것은 사실이 아니다. "." inside []는 (적어도 정규 표현식 구현에서는) 특별한 의미가 없습니다. 그것을 피할 수는 없지만 필수는 아닙니다. –

관련 문제