Jump to content

preg_replace and <ul>


immanuelx2

Recommended Posts

Hey all. I have been trying to figure this out for literally several hours and my head is starting to hurt. I come to you experts as a last resort.  I am by no means a regexp expert, in fact the format below is something I snagged off the internet, so it may be wrong...

 

So far I have this for my bbcode parsing:

<?php

$bbcode = array(
	'@\[url=(.*?)\](.*?)\[/url\]@si' => '<a href="\\1" target="_blank">\\2</a>',
	'@\[quote\](.*?)\[/quote\]@si' => '<i>"\\1"</i>',
	'@\[ul\](.*?)\[/ul\]@si' => '<ul>\\1</ul>' ,
	'@\[li\](.*?)\[/li\]@si' => '<li>\\1</li>' 
);

return preg_replace(array_keys($bbcode), array_values($bbcode), $string);

?>

 

However, when it comes to the "ul" and "li" tags... I want to omit any newline ("\n") characters before or after those elements, because I have a nl2br() statement that parses right at the end to output the HTML, and since <li> tags are naturally list-type elements, they don't need extra <br /> around them.

 

Just like in these forums:

[list]
[*]item 1
[*]item 2
[/list]

 

between items 1 and 2, and the two list tags, no additional line breaks are added, even though there are newlines created when that code is written. So my question is, how can I remove the newlines at those specific points only, so that my nl2br() can parse the rest of the newlines in my string as breaks?

 

Thanks in advance for any help

Link to comment
https://forums.phpfreaks.com/topic/159209-preg_replace-and/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.