开发者

How do I wrap blocks of li elements in ul elements

开发者 https://www.devze.com 2023-02-09 23:02 出处:网络
Okay considering having the following sample string. Duis posuere suscipit lacus quis lacinia. In molestie lectus sed.

Okay considering having the following sample string.

Duis posuere suscipit lacus quis lacinia. In molestie lectus sed.
<li>test line here</li>
<li>Second test</li>
<li>Third test</li>
<li>Another test</li>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis posuere suscipit lacus quis lacinia. In molestie lectus sed.
\* Aliquam posuere sapien id elit sodales at vestibulum lacus porttitor. 
*hej
A * hejdu
<li>hejsan</li>
<li>Another item</li>
Lore开发者_开发技巧m ipsum dolor sit amet, consectetur adipidsa
<li>Lonely list item</li>

So lets say I want to wrap the three li blocks in ul elements so I'll have the four first li elements wrapped in one ul, the two elements in the 2nd block wrapped in one ul and the last one wrapped in one ul. I'm thinking regex but I don't know quite how I'd build up such a regex. Maybe there's some other option?


If this is a source transformation, then a regular expression might be an option It depends on the format being consistent. But then you could use something like:

= preg_replace("#(^<li>.*?</li>\s*)+#m", "<ul>$0</ul>", $content);

The trick is looking for something that can occour multiple times with (...)+ while the inner part is restricted to exact matches <li>..</li> which even requires to be at the ^ start of a line.

There might be other options. But I cannot imagine how to wrap li-groups with DOM methods, since you start out with a text document.

0

精彩评论

暂无评论...
验证码 换一张
取 消