Jump to content

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

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.