The Little Guy Posted April 16, 2010 Share Posted April 16, 2010 I have the following method: public function format($text){ $text = preg_replace("~\[intro\](.*?)\[\/intro\]~si", "<h3>Introduction</h3>$1", $text); $text = preg_replace("~\[links\](.*?)\[\/links\]~si", '<h3>Links</h3>'.$this->setLinks("$1"), $text); $text = preg_replace("~\[qs\](.*?)\[\/qs\]~si", "<h3>Quick Steps</h3><pre>$1</pre>", $text); $text = preg_replace("~\[title=(.*?)\]~si", "<h3>$1</h3>", $text); $text = preg_replace("~\[img=(.+?)\]~si", "<a href=\"/images/dspl/$1\"><img src=\"/images/dspl/$1\" /></a>", $text); $text = nl2br($text); return $text; } I have nl2br, but there are some areas where I don't want to use it, such as between and after the "qs" tags, after the "title" tags, and after the "intro" tags. How can I do that? Quote Link to comment https://forums.phpfreaks.com/topic/198715-line-breaks/ Share on other sites More sharing options...
NTSmarkv Posted April 16, 2010 Share Posted April 16, 2010 function br2nl($string) { return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string); } That should simplify it for yah Quote Link to comment https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1042844 Share on other sites More sharing options...
oni-kun Posted April 16, 2010 Share Posted April 16, 2010 Why not trim or remove newline entities from the text you don't wish to be converted to <BR>? Quote Link to comment https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1042853 Share on other sites More sharing options...
The Little Guy Posted April 16, 2010 Author Share Posted April 16, 2010 Why not trim or remove newline entities from the text you don't wish to be converted to <BR>? How would I do that with my current code? Quote Link to comment https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1043106 Share on other sites More sharing options...
ChemicalBliss Posted April 16, 2010 Share Posted April 16, 2010 use concatenation on the replacement valuies for the tags you _do_ want a <br> after: public function format($text){ $text = preg_replace("~\[intro\](.*?)\[\/intro\]~si", "<h3>Introduction</h3>$1", $text); $text = preg_replace("~\[links\](.*?)\[\/links\]~si", '<h3>Links</h3>'.$this->setLinks("$1"), $text."<BR/>"); $text = preg_replace("~\[qs\](.*?)\[\/qs\]~si", "<h3>Quick Steps</h3><pre>$1</pre>", $text); $text = preg_replace("~\[title=(.*?)\]~si", "<h3>$1</h3>", $text); $text = preg_replace("~\[img=(.+?)\]~si", "<a href=\"/images/dspl/$1\"><img src=\"/images/dspl/$1\" /></a>", $text)."<BR/>"; return $text; } -cb- Quote Link to comment https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1043111 Share on other sites More sharing options...
The Little Guy Posted April 16, 2010 Author Share Posted April 16, 2010 use concatenation on the replacement valuies for the tags you _do_ want a <br> after: public function format($text){ $text = preg_replace("~\[intro\](.*?)\[\/intro\]~si", "<h3>Introduction</h3>$1", $text); $text = preg_replace("~\[links\](.*?)\[\/links\]~si", '<h3>Links</h3>'.$this->setLinks("$1"), $text."<BR/>"); $text = preg_replace("~\[qs\](.*?)\[\/qs\]~si", "<h3>Quick Steps</h3><pre>$1</pre>", $text); $text = preg_replace("~\[title=(.*?)\]~si", "<h3>$1</h3>", $text); $text = preg_replace("~\[img=(.+?)\]~si", "<a href=\"/images/dspl/$1\"><img src=\"/images/dspl/$1\" /></a>", $text)."<BR/>"; return $text; } -cb- That doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1043253 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 In the preg_replace, you can match the newline character (the ones you don't want replaced), then replace it with some dummy text, but make it distinct. Something like {LINE_BREAK}. Then after nl2br, replace {LINE_BREAK} with the newline character. Quote Link to comment https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1043255 Share on other sites More sharing options...
The Little Guy Posted April 16, 2010 Author Share Posted April 16, 2010 Am I doing this correctly? I cant get it to work... public function format($text){ //$text = nl2br($text); $text = preg_replace("~\n~", "{LINE_BREAK}", $text); $text = preg_replace("~\[intro\](.*?)\[\/intro\]~si", "<h3>Introduction</h3>$1", $text); $text = preg_replace("~\[links\](.*?)\[\/links\]~si", '<h3>Links</h3>'.$this->setLinks("$1"), $text); $text = preg_replace("~\[qs\]({LINE_BREAK})\[\/qs\]~si", "\n", $text); $text = preg_replace("~\[qs\](.*?)\[\/qs\]~si", "<h3>Quick Steps</h3><pre>$1</pre>", $text); $text = preg_replace("~\[title=(.*?)\]~si", "<h3>$1</h3>", $text); $text = preg_replace("~\[img=(.+?)\]~si", "<a href=\"/images/dspl/$1\"><img src=\"/images/dspl/$1\" /></a>", $text); $text = preg_replace("~{LINE_BREAK}~s", "<br />", $text); return $text; } Quote Link to comment https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1043270 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 No, no. You need to be specific. You said you don't want it replace in between the qs tag and after it. Just focus on those special cases. After that, run it through nl2br. It'll put breaks in all places except those you don't want because you replaced those values. After you run it through nl2br, then you change {LINE_BREAK} back tot he new line character. Quote Link to comment https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1043273 Share on other sites More sharing options...
The Little Guy Posted April 17, 2010 Author Share Posted April 17, 2010 I still can't get it. how can I replace ONLY new lines between the qs tags? Quote Link to comment https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1043517 Share on other sites More sharing options...
The Little Guy Posted April 17, 2010 Author Share Posted April 17, 2010 I just used the e modifier: $text = preg_replace("~\[qs\](.*?)\[\/qs\]~sei", "'<h3>Quick Steps</h3><pre>'.stripslashes(str_replace('<br />', '', '$1')).'</pre>'", $text); Quote Link to comment https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1043551 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.