Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_match has string size limit

Tags:

php

preg-match

preg_match has limit of str on PHP 5.2.5

<?php
    $str1 = 'a@b%c@d' . str_repeat ('=', 33326);
    $str2 = 'a@b%c@d' . str_repeat ('=', 33327);
    $regexp = '/^(.*)@(.*)%(.*)$/si';

    echo preg_match ($regexp, $str1) ? "Correct " : "Wrong ";  // works correctly
    echo "\n";
    echo preg_match ($regexp, $str2) ? "Correct " : "Wrong ";  // exhibits the bug
    echo "\n";
like image 593
freddiefujiwara Avatar asked May 30 '11 07:05

freddiefujiwara


People also ask

What is the difference between Preg_match and Preg_match_all?

preg_match stops looking after the first match. preg_match_all , on the other hand, continues to look until it finishes processing the entire string. Once match is found, it uses the remainder of the string to try and apply another match.

What is the use of Preg_match?

PHP | preg_match() Function. This function searches string for pattern, returns true if pattern exists, otherwise returns false. Usually search starts from beginning of subject string. The optional parameter offset is used to specify the position from where to start the search.

What value is return by Preg_match?

Return Values ¶ preg_match() returns 1 if the pattern matches given subject , 0 if it does not, or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false . Please read the section on Booleans for more information.

What is pattern matching in PHP?

preg_match() in PHP – this function is used to perform pattern matching in PHP on a string. It returns true if a match is found and false if a match is not found. preg_split() in PHP – this function is used to perform a pattern match on a string and then split the results into a numeric array.


1 Answers

preg_last_error() after the second call returns 2 (=PREG_BACKTRACK_LIMIT_ERROR) so you might want to raise this value.

like image 93
user187291 Avatar answered Oct 28 '22 18:10

user187291