Jump to content

line breaks


The Little Guy

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/198715-line-breaks/
Share on other sites

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-

Link to comment
https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1043111
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1043253
Share on other sites

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;
}

Link to comment
https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1043270
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/198715-line-breaks/#findComment-1043273
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.