James Mathieson
James Mathieson

Reputation: 439

Regex to match only the first listed item in a block of HTML

My CMS allows PHP keyword replacements, and I'm currently building a format to return the first listed item element in a data field which usually contains a HTML unordered list, but can often contain paragraphs, etc.

If possible, I'd like to use a regular expression to match only the first listed item element li in a returned block, and print it.

One severe limitation, is that I cannot use the ^ character as my CMS (annoyingly) uses that character for modification functions.

So far, I've only come up with: replace:<\/li>.*:</li></ul> - but this is only replacing the first listed item's closing tag in a returned block. What I really need is something like:

replace:anything_that's_not_first_li_element:nothing

I appreciate that this question is a very long shot, so thanks in advance for all constructive responses.

Upvotes: 1

Views: 824

Answers (1)

Bryan Elliott
Bryan Elliott

Reputation: 4095

You could use this regex with the s flag.

(?<=<ul>).*?<li>.*?<\/li>

Working regex example:

http://regex101.com/r/hL1zF0

PHP:

$list = '<ul>
<li>first</li>
<li>second</li>
<li>third</li>
<li>fourth</li>
</ul>';

preg_match('/(?<=<ul>).*?<li>.*?<\/li>/s', $list, $matches);

echo $matches[0];

Output:

<li>first</li>

Upvotes: 1

Related Questions