Apply PHP regex replace on a multi-line repeted pattern
Posted
by Hussain
on Stack Overflow
See other posts from Stack Overflow
or by Hussain
Published on 2010-05-01T04:45:05Z
Indexed on
2010/05/01
4:47 UTC
Read the original article
Hit count: 279
Let's say I have this input:
I can haz a listz0rs!
# 42
# 126
I can haz another list plox?
# Hello, world!
# Welcome!
I want to split it so that each set of hash-started lines becomes a list:
I can haz a listz0rs!
<ul>
<li>42</li>
<li>126</li>
</ul>
I can haz another list plox?
<ul>
<li>Hello, world!</li>
<li>Welcome!</li>
</ul>
If I run the input against the regex "/(?:(?:(?<=^# )(.*)$)+)/m", I get the following result:
Array
(
[0] => Array
(
[0] => 42
)
Array
(
[0] => 126
)
Array
(
[0] => Hello, world!
)
Array
(
[0] => Welcome!
)
)
This is fine and dandy, but it doesn't distinguish between the two different lists. I need a way to either make the quantifier return a concatenated string of all the occurrences, or, ideally, an array of all the occurrences.
Idealy, this should be my output:
Array
(
[0] => Array
(
[0] => 42
[1] => 126
)
Array
(
[0] => Hello, world!
[1] => Welcome!
)
)
Is there any way of achieving this, and if not, is there a close alternative?
Thanks in advance!
© Stack Overflow or respective owner