簡體   English   中英

檢查字符串的第一個字符是否為數字並且字符串中沒有字母

[英]Check that the first character of the string is a digit and there are no letters in the string

如何檢查字符串的第一個字符是數字並且字符串中沒有字母?

is_int(intval(substr($string, 0, 1))) && !preg_match('\w*}', $string)

這個檢查是行不通的!

您正在尋找的正則表達式是^[0-9]\P{L}+$ ,您不需要u標志。

<?php
function check($text) {
    if (($result = preg_match('/^[0-9]\P{L}+$/', $text)) !== false) {
        return $result;
    }
    throw new Exception("Error with regex");
}

check('123')    // true
check('1$%#@')  // true
check('12д54')  // false
check('1łs')    // false
check('')       // false

此外,使用開源T-Regx ,您可以執行以下操作:

<?php
pattern('^[0-9]\P{L}+$')->test('123');  // true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM