www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-07-2009, 05:46 PM
    pastet89 pastet89 is offline
    Registered User
     
    Join Date: Sep 2008
    Posts: 40
    Regular expressions: Match a repeating string

    Hello all!
    I have that string:
    Code:
    $str = "blablabla 00 00 bla 11 blablabla 22 blablabla 33 33 33 33 blabla 44 bla";
    and I want to get that output from it:
    Code:
    Array
    (
        [0] => Array
            (
                [0] => bla 00 00 bla
                [1] => bla 11 bla
                [2] =>  bla 22 bla
                [3] => bla 33 33 33 33 bla
            )
     
        [1] => Array
            (
                [0] => 00 00
                [1] => 11
                [2] => 22
                [3] =>  33 33 33 33 
            )
     
    )
    The [0] array is not important for me. I know it always will contain the whole matches. But what I need is in the second array [1], to get all the numbers, REPATING OR NOT. I use that code:
    Code:
    preg_match_all("#" . "bla([[:space:]]*[0-9]{2}[[:space:]]*)*bla" . "#", $str, $ma4);
    print_r($ma4);
    and what I get is:
    Code:
    Array
    (
        [0] => Array
            (
                [0] => blabla
                [1] => bla 00 00 bla
                [2] => blabla
                [3] => bla 22 bla
                [4] => blabla
                [5] => blabla
            )
     
        [1] => Array
            (
                [0] => 
                [1] => 00 
                [2] => 
                [3] =>  22 
                [4] => 
                [5] => 
            )
     
    )
    Please, help me to get the code I need!
    Reply With Quote
      #2  
    Old 11-07-2009, 06:57 PM
    NogDog's Avatar
    NogDog NogDog is online now
    High Energy Magic Dept.
     
    Join Date: Aug 2004
    Location: Ankh-Morpork
    Posts: 13,641
    I'd probably try something like this (untested):
    Code:
    '/\b([0-9]{2})\b(\s+\1\b)*/'
    __________________
    "That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett
    freelancer.internet.com
    Email me
    Reply With Quote
      #3  
    Old 11-07-2009, 07:28 PM
    pastet89 pastet89 is offline
    Registered User
     
    Join Date: Sep 2008
    Posts: 40
    nope, Here is the output:
    Code:
    Array
    (
        [0] => Array
            (
                [0] => 00
                [1] => 00
                [2] => 11
                [3] => 22
                [4] => 33
                [5] => 33
                [6] => 33
                [7] => 33
                [8] => 44
            )
     
        [1] => Array
            (
                [0] => 00
                [1] => 00
                [2] => 11
                [3] => 22
                [4] => 33
                [5] => 33
                [6] => 33
                [7] => 33
                [8] => 44
            )
     
        [2] => Array
            (
                [0] => 
                [1] => 
                [2] => 
                [3] => 
                [4] => 
                [5] => 
                [6] => 
                [7] => 
                [8] => 
            )
     
    )
    Reply With Quote
      #4  
    Old 11-07-2009, 08:01 PM
    NogDog's Avatar
    NogDog NogDog is online now
    High Energy Magic Dept.
     
    Join Date: Aug 2004
    Location: Ankh-Morpork
    Posts: 13,641
    Works for me:
    PHP Code:
    <?php
    $str
    = "blablabla 00 00 bla 11 blablabla 22 blablabla 33 33 33 33 blabla 44 bla";
    preg_match_all('/\b([0-9]{2})\b(\s+\1\b)*/', $str, $matches);
    echo
    "<pre>".print_r($matches[0], 1)."</pre>";
    Output:
    Code:
    Array
    (
        [0] => 00 00
        [1] => 11
        [2] => 22
        [3] => 33 33 33 33
        [4] => 44
    )
    __________________
    "That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett
    freelancer.internet.com
    Email me
    Reply With Quote
      #5  
    Old 11-07-2009, 08:37 PM
    pastet89 pastet89 is offline
    Registered User
     
    Join Date: Sep 2008
    Posts: 40
    Wow, it really seems to work!
    But can you help me a little bit further, because that was just a simple example of what am I trying to do and now I tried to apply it to my project and I coludn't...
    OK, I have that string:
    Code:
    <td>team1</td>
    <td><img src='/img/b.png' class='redYellowCard' alt='' \> team2</td>
    <td><img src='/img/b.png' class='redYellowCard' alt='' \> <img src='/img/b.png' class='redCard' alt='' \> team3</td>
    The array I want to get is:
    Code:
       Array
    (
        // the whole matches
        [0] => Array
            (
                [0] => team1
                [1] => <img src='/img/b.png' class='redYellowCard' alt='' \> team2
                [2] => <img src='/img/b.png' class='redYellowCard' alt='' \> <img src='/img/b.png' class='redCard' alt='' \> team3 
            ) 
        //the images independently of their number - 0, 1, 2, 3 etc. For the simple expample, I give the maximum of 2 
        [1] => Array
            (
                [0] => 
                [1] => <img src='/img/b.png' class='redYellowCard' alt='' \>
                [2] => <img src='/img/b.png' class='redYellowCard' alt='' \> <img src='/img/b.png' class='redCard' alt='' \>
            )
        //the teams
        [2] => Array
            (
                [0] => team1
                [1] => team2
                [2] => team3
            )
     
    )
    First I tried that one:
    Code:
    preg_match_all(
    "#" . "(<[a-z][a-z0-9]*[^<>]*>)" . "([[:space:]]*\<img src='/img/b.png' class\='[redYlowCard]+' alt\='' /\>[[:space:]]*)*" . "([a-z0-9]+)" . "(</[a-z][a-z0-9]*[^<>]*>)"
    )
    and it was matching only zero to maximum one image.
    Now I try to apply your code in that way:
    Code:
    preg_match_all(
    "#" . "(<[a-z][a-z0-9]*[^<>]*>)" . "/\b<img src='/img/b.png' class\='[redYlowCard]+' alt\='' /\>\b(\s+\1\b)*/" . "([a-z0-9]+)" . "(</[a-z][a-z0-9]*[^<>]*>)"
    )
    and there is no match at all!
    Can you help me to apply it properly?

    Last edited by pastet89; 11-07-2009 at 08:42 PM.
    Reply With Quote
      #6  
    Old 11-08-2009, 07:08 AM
    NogDog's Avatar
    NogDog NogDog is online now
    High Energy Magic Dept.
     
    Join Date: Aug 2004
    Location: Ankh-Morpork
    Posts: 13,641
    I would agree with the reply you got at PHPBuilder that you use the DOM class/functions for this.
    __________________
    "That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett
    freelancer.internet.com
    Email me
    Reply With Quote
      #7  
    Old 11-08-2009, 08:54 AM
    pastet89 pastet89 is offline
    Registered User
     
    Join Date: Sep 2008
    Posts: 40
    ... I have just fixed it without them!!!
    Thanks a lot for the initial help!!!
    Here is my code:
    Code:
    ((?:[[:space:]]*<img src='/img/b.png' class\='[redYlowCard]+' alt\='' /\>?[[:space:]]*)*)
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 12:31 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.