0

Im trying to search for nn/nnnnnn/nn in a file..

I have this

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

which deals with the centre 6 numbers but how do I get it to search for the whole pattern? I get a little confused when i have to add the search strings together. I know I have to do the following

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

HOw do I add the search string as one? Thanks in advance Cheers Mat

4 Answers 4

2

You can also use \d for a digit instead:

preg_match_all("#(\d{2})/(\d{6})/(\d{2})#", $string, $matches);
print_r($matches);
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried:

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

If searching 12/123456/12, for example, it will output:

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

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

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

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

)

3 Comments

You're forgetting the end modifier slash. I'd also prefer to use another modifier, so you don't have to escape the slash: preg_match_all("#([0-9]{2})/([0-9]{6})/([0-9]{2})#")
Noticed that just before your comment!
@h2ooooooo You don't mean "modifier", you mean "delimiter"; just for future clarity. A "modifier" would be something you added onto the end of the string, outside the "delimiters" which mark the start and end of the pattern itself.
0

Try this #(\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);
}

Returns:

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

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

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

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

)

Comments

0

How about using #([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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.