To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > General Help

General Help Forum for General Help questions pertaining to PHP

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-07-2009, 06:46 PM   #1
pastet89
Member
 
Join Date: Sep 2008
Posts: 42
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!
pastet89 is offline   Reply With Quote
Old 11-07-2009, 10:46 PM   #2
nrg_alpha
Personally Humbled Person
 
nrg_alpha's Avatar
 
Join Date: Mar 2008
Location: Canada
Posts: 893
The pattern I came up with that closest mimics what you seek would be as follows:

Example:
PHP Code:
$str = "blablabla 00 00 bla 11 blablabla 22 blablabla 33 33 33 33 blabla 44 bla";
preg_match_all('#bla\s+((?:\d+\s+)+)(?=bla)#i', $str, $matches);;
$matches[1] = array_map('rtrim', $matches[1]); // remove right spaces from array elements
array_pop($matches[1]); // remove the final array element (in this case, 44) from array
echo '<pre>'.print_r($matches[1], true);
Output:
Code:
Array
(
    [0] => 00 00
    [1] => 11
    [2] => 22
    [3] => 33 33 33 33
)
Obviously, if you wanted to include that final number 44 array element value, don't include the array_pop line. I also used the i modifier to make it all case insensitive (just incase the blah part is upper or lower case.. if this is not needed, simply remove that modifier.
__________________
"Build a man a fire, you warm him for a day. Set a man on fire, and he'll be warm for the rest of his life."
------------------------------------------------------------------------------------------------------
If preg was a woman, I'd marry her. But I could just see the pattern... she would replace me with someone with money ($1).
nrg_alpha is offline   Reply With Quote
Old 11-07-2009, 11:12 PM   #3
pastet89
Member
 
Join Date: Sep 2008
Posts: 42
Thanks a lot...Strange to me, because I am working with regular expressions for a long time, but I can not read your regex code and apply it to my real project! Can you help me with that? Here goes the real project:
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>
Array needed:
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
        )
 
)
I was using that code trying to get it:
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]*[^<>]*>)"
)
but it was matching MAXIMUM one image!
I would really appreciate if you help me to apply your code to that project.
pastet89 is offline   Reply With Quote
Old 11-08-2009, 03:55 AM   #4
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,122
Have you considered using DOM to parse the HTML? Then you wouldn't have to parse it yourself.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 11-08-2009, 09:54 AM   #5
pastet89
Member
 
Join Date: Sep 2008
Posts: 42
... 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:]]*)*)
pastet89 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 -4. The time now is 09:34 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.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.