Hi guys,
how can I match a block of
<li>item 1</li>
<li>item 2</li>
no matter there is a blank line before or after the block
and enclose it in <ul></ul>
tags using PHP's preg_*
functions.
Thank you for answer
Hi guys,
how can I match a block of
<li>item 1</li>
<li>item 2</li>
no matter there is a blank line before or after the block
and enclose it in <ul></ul>
tags using PHP's preg_*
functions.
Thank you for answer
Use an XML parser to do this, not a regex. PHP has one built in.
David already stated, that php got xml and html-parsers. However if you really want to use a regex, it probably would be something like:
preg_match('#<li>(.*?)</li>#', $string);
// Same thing
preg_match('#<li>(.*)</li>#U', $string);
If this is safe, controlled input, and you just got LIs with missing parent ULs, you can do:
preg_replace ( '#\s*(?:<li>.*</li>\s*)+#' , '<ul>$0</ul>', $input )
(You may want to add some \n
to the replacement string before or after the UL.)
NOTE: This will fail if:
.
excludes newline by default).Some of these can relatively easily be catered for, but I'm not going to - if you haven't got known specific content, you should be using a real HTML parser instead.
The 'Regular' in Regular Expressions has a specific meaning, and full HTML is not a Regular language, so trying to handle all the intricacies of HTML with simple regex is liable to fail.
If you use a bad regex on user-supplied HTML, you may be introducing HTML injection vulnerabilities into your code.