PHP模式搜索

时间:2013-01-19 17:39:53

标签: php

我试图在文件中搜索nn / nnnnnn / nn ..

我有这个

if (preg_match_all("([0-9]{6})", $file, $out)) {
echo "Match was found <br />";
print_r($out);

处理中心6号码,但如何让它搜索整个模式? 当我必须将搜索字符串添加到一起时,我有点困惑。 我知道我必须做以下事情

([0-9] {2})和/和([0-9] {6})和/和([0-9] {2})

我是否将搜索字符串添加为一个? 提前致谢 干杯 垫

4 个答案:

答案 0 :(得分:2)

您也可以使用\d代替数字:

preg_match_all("#(\d{2})/(\d{6})/(\d{2})#", $string, $matches);
print_r($matches);

答案 1 :(得分:0)

你试过了吗?

preg_match_all("/([0-9]{2})\/([0-9]{6})\/([0-9]{2})/")

例如,如果搜索12/123456/12,则会输出:

Array
(
    [0] => Array
        (
            [0] => 12/123456/12
        )

    [1] => Array
        (
            [0] => 12
        )

    [2] => Array
        (
            [0] => 123456
        )

    [3] => Array
        (
            [0] => 12
        )

)

答案 2 :(得分:0)

试试这个#(\d{2})/(\d{6})/(\d{2})#

$file = '23/334324/23';
if (preg_match_all("#(\d{2})/(\d{6})/(\d{2})#", $file, $out)) {
    echo "Match was found <br />";
    print_r($out);
}

返回:

Array
(
    [0] => Array
        (
            [0] => 23/334324/23
        )

    [1] => Array
        (
            [0] => 23
        )

    [2] => Array
        (
            [0] => 334324
        )

    [3] => Array
        (
            [0] => 23
        )

)

答案 3 :(得分:0)

如何使用#([0-9]{2})/([0-9]{6})/([0-9]{2})#

if(preg_match_all("#([0-9]{2})/([0-9]{6})/([0-9]{2})#", $file, $out)) 
{
    echo "Match was found <br />";
    print_r($out);
}