Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation preg_match() maximum string length reached

    I am using PHP 5.2.0 on Apache 2.0 and trying to get preg_match() to scan a certain result string returned. However, it wouldn't match the regular expression with the data. I verified the expression is valid in RegexBuddy. I also tested it again in PHP with a shorter string containing approximately the same kind of content and it worked fine.

    After some very tedious testing I found that preg_match() simply stops working when the string is 100,055 chars in length. No errors, warnings, or notices. It just won't do the match if there are that many chars or more.

    I have never experienced this with early versions of PHP and it is really causing problems.

    Does anyone know how to resolve this issue?

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I dunno, explode your string into array by strpos max character and then do your pregmatch for each and then put it back together LOL.

  • #3
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    That won't work unfortunately. Each result has a serious of delimiting tags used to identify where each data segment begins. The contents of each tags has to be unserialized after being captured. Each data segment could in and of itself go over the 100,055 char limit.
    Last edited by bauhsoj; 03-29-2007 at 07:31 PM.

  • #4
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    count by /n or w/e your delimeter is. Then take a best guess on max char and go way under it for a buffer. Then for every 100 delimeters lets start a new array.

    But I don't think you should be having that problem, have you checked to see if its a bug?

    Side note, how well does something that large perform? Time to upgrade to the db?

  • #5
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Maybe a series of strpos and substr calls and some custom logic solves the issue.

    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #6
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    You should probably question whether you should really be using regular expressions on such a long string, as they aren't the most efficient of things.

  • #7
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    PCRE has some limitations as to backreference length and match length which can depend on the options it was compiled with and the OS it is running on.
    Regex might not be suitable for what you want it to do.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #8
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dumpfi View Post
    Maybe a series of strpos and substr calls and some custom logic solves the issue.

    dumpfi
    Worked like a charm!

    I got so wrapped up in the simplicity of just writing a regular expression to handle it that it didn't even occur to me to just write a simple algorithm like that.

    Here is what I came up with to solve the issue in case anyone might find it useful:
    PHP Code:
    $result_tags = array('codes''errors''lines');
    $returned_result_data = array();

    if (!empty(
    $results)) {
        foreach (
    $result_tags as $tag) {
            
    $opening_tag_pos strpos($results"<$tag>");
            
            if (
    $opening_tag_pos !== false) {
                
    $closing_tag_pos strpos($results"</$tag>");
                
                if (
    $closing_tag_pos !== false) {
                    
    $tag_data_begins strlen("<$tag>") + $opening_tag_pos;
                    
                    
    $tmp_extracted_data substr($results$tag_data_begins$closing_tag_pos 1);
                    
                    if (
    $tmp_extracted_data != '') {
                        
    $returned_result_data[$tag] = $tmp_extracted_data;
                        
                        
    $tmp_unserialized = @unserialize($returned_result_data[$tag]);
                        
                        if (
    $tmp_unserialized !== false || strpos($tmp_extracted_data'bool') !== false)
                            
    $returned_result_data[$tag] = $tmp_unserialized;
                    }
                }
            }
        }

    Last edited by bauhsoj; 03-30-2007 at 06:24 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •