[英]PHP Regex - optional Unicode UTF-8 character (Ø?) is not working
[英]utf-8 character input fail to PHP regex
<?php
if(isset($_GET['textvalue'])){
$string = $_GET['textvalue']; //preg_match return false
//$string = '한자漢字メ'; //preg_match return true
$stringArray = preg_match('/^[\p{L}]{2,30}$/u', $string);
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="GET">
<input type="text" name="textvalue">
<input type="submit">
</form>
</body>
</html>
我正在嘗試從輸入中復制值。
不幸的是,每次提交字符時, preg_match
返回false
。 但是,如果我使用變量中的字符串,它將返回true
。
發生了什么,我該如何解決?
如果有人遇到這個問題,我已經找到了。 您只需要添加此元標頭:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
我不知道為什么,但是沒有上面的代碼,html它將值作為非utf-8值發送給php。 所以,然后preg_match嘗試讀取它,它讀取的值與輸入的值不同,因此; 它返回false。
這就是你只使用字符串時它的工作原理。 HTml不參與。
注意。 即使您嘗試通過回顯它來閱讀,html也會將其恢復為原始的utf-8值。 奇怪的。
例:
<?php
if(isset($_GET['textvalue'])){
$string = $_GET['textvalue']; //preg_match return false
//$string = '한자漢字メ'; //preg_match return true
$stringArray = preg_match('/^[\p{L}]{2,30}$/u', $string);
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<body>
<form method="GET">
<input type="text" name="textvalue">
<input type="submit">
</form>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.