Reputation: 439
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
Reputation: 4095
You could use this regex with the s
flag.
(?<=<ul>).*?<li>.*?<\/li>
Working regex example:
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