Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.