Jump to content

Some preg_replace help, please.


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).

 

The idea is that if I encounter the [nobr] tag, I need to remove all line breaks. Here's the best I can do, but it's not working as I had hoped.

 

Here's what I currently use to replace line breaks with a break tag.

$str=str_replace("\r\n","<br>",$str);

 

Here's my attempt at introducing this new tag.

function BbToHtml($str) {

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

$during=str_replace("\r\n","",$during);

if($during != "") { // If the [nobr] tag was found, go with the new string.
  $str = $before.$during.$after;
}
else { // If the [nobr] tag was not found, go ahead like normal.
  $str=str_replace("\r\n","<br>",$str);
}

return $str; 

 

I can't get that working. Any ideas on how to fix that, or a completely alternate method of doing it? Thank you!

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.