Skip to content

Unexpected behavior of preg_match #8316

Closed
@great-handsome

Description

@great-handsome

Description

The following code:

<?php
$clean_text = "22ABCDE1ABCDE2ABCDE3ABCDE4ABCDE5ABCDESubtotalABCDE1ABCDE11111ABCDE111111ABCDE22222222222222222222222222222222222ABCDE111111111111111ABCDE211111111111111111111111ABCDE111111111111111111111111ABCDE11111111111111111111111111111ABCDE1111111111ABCDE11111111111111ABCDE11111111111111111111111111111111111111111111111111111";

if( preg_match( '/(.*)ABCDE(.*)ABCDE(.*)ABCDE(.*)ABCDE(.*)ABCDE(.*)ABCDESubtotal/',$clean_text, $data_d ))
	print_r($data_d);
else
	echo 'NO';

Resulted in this output:

NO

But I expected this output instead:

Array
(
    [0] => 22ABCDE1ABCDE2ABCDE3ABCDE4ABCDE5ABCDESubtotal
    [1] => 22
    [2] => 1
    [3] => 2
    [4] => 3
    [5] => 4
    [6] => 5
)

If I delete the last char from $clean_text ( or one char from 22222222222222222222222222222222222 ) , preg_match returns TRUE.

I tried on PHP 7.4 and 8.1 , on 2 servers.

PHP Version

PHP 8.1

Operating System

Ubuntu : Linux core 5.3.0-47-generic #39~18.04.1-Ubuntu SMP Mon Apr 6 17:04:32 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions