3.3. 数字

未匹配的标注

1、匹配数字

元字符 \d[0-9]含义相同。

<?php

//字符串
$str = '1a2b3c';

//正则表达式
$regular = '/\d/';

//执行匹配正则表达式
preg_match_all($regular, $str, $matches);

//打印结果
echo '<pre>';
print_r($matches);
echo '</pre>';

输出结果

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)

2、匹配非数字

元字符 \D[^0-9]含义相同。

<?php

//字符串
$str = '1a2b3c';

//正则表达式
$regular = '/\D/';

//执行匹配正则表达式
preg_match_all($regular, $str, $matches);

//打印结果
echo '<pre>';
print_r($matches);
echo '</pre>';

输出结果

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

)

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~