次の関数を使用して、数字のみを許可しています。
if (empty($VAT) || (!(ctype_digit($VAT)))) {
$mistakes[] = 'ERROR - Your title is either empty or should only contain NUMBERS starting with a 4.';
この関数を追加/変更して 10 桁のみを受け入れ、数字 4 で始まる必要がある方法はありますか?
次の関数を使用して、数字のみを許可しています。
if (empty($VAT) || (!(ctype_digit($VAT)))) {
$mistakes[] = 'ERROR - Your title is either empty or should only contain NUMBERS starting with a 4.';
この関数を追加/変更して 10 桁のみを受け入れ、数字 4 で始まる必要がある方法はありますか?
preg_match()
あなたが探しているものです:
<?php
header('Content-Type: text/plain; charset=utf-8');
$number1 = '4123456789';
$number2 = '3123456789';
$regex = '/^4\d{9}$/';
// ^ test pattern: 4 in begining, at least 9 digits following.
echo $number1, ': ', preg_match($regex, $number1), PHP_EOL;
echo $number2, ': ', preg_match($regex, $number2), PHP_EOL;
?>
出力:
4123456789: 1
3123456789: 0
更新されたソース:
if (!preg_match('/^4\d{9}$/', $VAT)) {
$mistakes[] = 'ERROR - Your title is either empty or should only contain NUMBERS starting with a 4.';
}
可変桁数の場合は、次の正規表現を使用します: '/^4\d{1,9}$/'
.
preg_match
一致またはブール値を使用して返す
preg_match('/^[4]{1}[0-9]{9}$/', $VAT, $matches);
代替手段:
$VAT = "4850999999";
if (preg_match('/^[4]{1}[0-9]{9}$/', $VAT))
echo "Valid";
else
echo "Invalid";
^[4]
数字の 4 (4) から始めます
{1}
初期数制限
[0-9]
許可された文字
{9}
最初の数字の後に必要な 9 単位の数字
最善の方法ではないかもしれませんが、REGEX を使用すると実行できます。
これは一つの方法です
preg_match('/4[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/', $string, $matches);
$string はチェックする文字列、$matches は一致する結果が保存される場所です。