2016-12-21 5 views
1

mb_strlen : 16게시물 값에서 mb_strlen 및 strlen이 잘못된 이유 PHP? 내가 버튼을 제출 유형 <code>text</code> 이름 <code>text</code> 및 프레스 입력 요소에 <code>漢字</code>을 채우기 코드, 그 쇼와

<?php 
include("connect.php"); 
if(isset($_POST["submit"])) 
{ 
    $string = mysqli_real_escape_string($db_mysqli,$_POST['text']); 
    //$string = "漢字"; 

    echo $string."<BR>"; 
    echo "mb_strlen : ".mb_strlen($string, 'utf-8')."<BR>"; 
    echo "strlen : ".strlen($string)."<BR>"; 

    if(strlen($string) != mb_strlen($string, 'utf-8')) 
    { 
     echo "Please enter English words only:("; 
    } 
    else 
    { 
     echo "OK, English Detected!"; 
    } 
} 
?> 

<form method="post" ENCTYPE = "multipart/form-data"> 
<input type="text" name="text"> 
<input type="submit" name="submit" value="OK" id="button-blue" style=" float: none; "> 
</form> 

strlen : 16 그러나 사용이 코드가 표시됩니다의 mb_strlen : 2strlen : 6

위의 코드의 값이 잘못되어 적용하는 방법을 알고 싶습니다.

<?php 
    $string = "漢字"; 

    echo $string."<BR>"; 
    echo "mb_strlen : ".mb_strlen($string, 'utf-8')."<BR>"; 
    echo "strlen : ".strlen($string)."<BR>"; 

    if(strlen($string) != mb_strlen($string, 'utf-8')) 
    { 
     echo "Please enter English words only:("; 
    } 
    else 
    { 
     echo "OK, English Detected!"; 
    } 
?> 
+0

오직 팁이 있습니다 : 두 번째 예제에서 PHP 파일 인코딩은 파일에''漢字 ''를 직접 써주기 때문에 역할을합니다. – JustOnUnderMillions

+2

그리고 여기에 표시된 답변을 참조하십시오. http://stackoverflow.com/questions/8250709/mb-strlen-strlen-dont-return-correct-values-from-an-ajax-call-to-php – JustOnUnderMillions

+0

보낸 데이터와 비슷합니다. 귀하의 클라이언트가 서버에서 유니 코드로 올바르게 처리하지 않았습니다. – arkascha

답변

0

가능성이 대답-나중에 개정을-하지만 필요합니다 대신 strlen을 사용하는 우리는 입력 문자열이 비 라틴 문자가 있는지 확인하는 정규식을 사용하여 일부 망 가지고있다.

코드 :

$string = '漢字'; 
$matches = array(); 
$pattern = '/^[^\p{Latin}]+$/u'; 
preg_match($pattern, $string, $matches); 
print_r($matches); 

결과 :

Array 
(
    [0] => 漢字 
) 

내가 This is a Latin string [email protected]##$&()@!!! 테스트하면 내가 다시는 하늘의 배열을 얻는다. 나는 이것이 절대 안전한 솔루션이라고 생각하지 않지만 좋은 첫 걸음이라고 생각합니다.

정규식의 라틴 문자 범위는 U + 0000-U + 007F입니다. 이 Regex Tutorial Page은 유니 코드에 대해 자세히 설명합니다. 또한 유니 코드의 경우 내 패턴에 u 플래그가 있습니다. 그것이 포함될 필요가 있습니다.