Jump to content

preg_replace to remove line breaks between two tags?


brady123

Recommended Posts

I currently have some custom tags, similar to BBCode, to be used for comments on my site. I'm trying to create a [nobr] tag (note: I know the no break tag will work in many major browsers, but it's deprecated, so I'm trying to be compliant) to remove all line breaks between the opening and closing tag.

 

The idea is that if I encounter the [nobr] tag, I need to remove all line breaks between the opening and closing tags. Here's the best I could come up with, but it doesn't work. I can't think of anything else! :( Can anyone shed some light and help me out.  I could be way off here...

 

Note: the new part I'm working on is where you see the before, during, after. The other part of the function is for reference of what I'm working with thus far.

 

function BbToHtml($str) {

$pattern = array (
          "/\[b\](.+)\[\/b\]/Usi",
          "/\[i\](.+)\[\/i\]/Usi",
          "/\[u\](.+)\[\/u\]/Usi",
          "/\[color=(\#[0-9A-F]{6}|[a-z]+)\](.*)\[\/color\]/Usi",
          "/\[size=(.*)\](.*)\[\/size\]/Usi",
          "/\[img=(.*)\](.*)\[\/img\]/Usi",
          "/\[url=(.*)\](.+)\[\/url\]/Usi",
          );

$replace = array (
          "<strong>\\1</strong>",
          "<em>\\1</em>",
          "<u>\\1</u>",
          "<span style=\"color:\\1\">\\2</span>",
          "<img src='\\1' alt='\\2' border=\"0\"/>",
          "<a href='\\1' target='_new'>\\2</a>",
          );

$before=preg_replace("(.*)/\[nobr\](.*)\[\/nobr](.*)/Usie", "\\1", $str);
$during=preg_replace("(.*)/\[nobr\](.*)\[\/nobr](.*)/Usie", "\\2", $str);
$after=preg_replace("(.*)/\[nobr\](.*)\[\/nobr](.*)/Usie", "\\3", $str);

if($during != "") {
  $during=str_replace("\r\n","",$during);
  $str=$before.$during.$after;
}

$str=nl2br($str);
$str = preg_replace($pattern,$replace,$str);
return $str;
}

Link to comment
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.