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
https://forums.phpfreaks.com/topic/250267-some-preg_replace-help-please/
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.