|
|||||||
| PHP Discussion and technical support for using and deploying PHP based websites. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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"; 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
)
)
Code:
preg_match_all("#" . "bla([[:space:]]*[0-9]{2}[[:space:]]*)*bla" . "#", $str, $ma4);
print_r($ma4);
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] =>
)
)
|
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
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] =>
)
)
|
|
#4
|
||||
|
||||
|
Works for me:
PHP Code:
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 |
|
#5
|
|||
|
|||
|
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> 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
)
)
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]*[^<>]*>)" ) 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]*[^<>]*>)" ) Can you help me to apply it properly? Last edited by pastet89; 11-07-2009 at 08:42 PM. |
|
#6
|
||||
|
||||
|
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 |
|
#7
|
|||
|
|||
|
... 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:]]*)*) |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|