brady123 Posted November 2, 2011 Share Posted November 2, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/250267-some-preg_replace-help-please/ Share on other sites More sharing options...
MasterACE14 Posted November 2, 2011 Share Posted November 2, 2011 why not use nl2br() ? Quote Link to comment https://forums.phpfreaks.com/topic/250267-some-preg_replace-help-please/#findComment-1284150 Share on other sites More sharing options...
brady123 Posted November 2, 2011 Author Share Posted November 2, 2011 The snippet of the function I'm showing is part of a larger function, containing much more, which is why I'm using the str_replace for breaks. I'm trying to remove breaks though, not create them. Quote Link to comment https://forums.phpfreaks.com/topic/250267-some-preg_replace-help-please/#findComment-1284153 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.