[SOLVED]nl2br anything ouside [php] or [code]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

[SOLVED]nl2br anything ouside [php] or [code]

Post by vigge89 »

UPDATE: read newer replies for question

I'm currently creating a formatting function, which should format [ code ] and [ php ] into highlighted code

but I have a problem, before i highlight the text, I convert the text into htmlentities. This is a problem, becuase if i try to enter something with htmls special characters, they get converted to their entities, and therefore, they doesn't get highlighted by the script.

Example of input:

Code: Select all

ї php ]
<?php
echo "testing";
?>
&#1111;/ php ]
Output with htmlentities before highlighting is ran:

Code: Select all

<?php
<?php
echo "testing";
?>
?>
Output if htmlentites isn't called (how it should be):

Code: Select all

<?php
echo "testing";
?>
How can I make this work the best way?
Is there any way to only turn text outside [ php ], [ code ], etc. to htmlentities?

Any suggestions, answers or questions appreciated :)
Last edited by vigge89 on Sat Sep 25, 2004 2:50 pm, edited 3 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]html_entity_decode[/php_man] the content of the

Code: Select all

tag. pass it through the highlighter.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

yep, I used that before, but I also call nl2br before the highlighting, and I can't find any solution which only htmlentities and nl2brs text outside those tags. Also, wouldn't first changing html to entities and then back be kinda slow? just a thought, I got no idea if it is...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's slow-ish.. but it's easier than doing special processing for it..
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

[php_man]explode[/php_man]() the contents into an array, using

Code: Select all

,
,

Code: Select all

, and
as string separators. Then parse each part you want formatted. Then join the formatted, and non-formatted text together for output.

Hope it works.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Sami wrote:[php_man]explode[/php_man]() the contents into an array, using

Code: Select all

,
,

Code: Select all

, and
as string separators. Then parse each part you want formatted. Then join the formatted, and non-formatted text together for output.

Hope it works.
You mean that i should explode the text by

Code: Select all

?
Got no idea what you mean
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Neither do I.

I'm at the end of a very long work shift. And by very long I mean spanning across multiple days.

Holy hell I need sleep. :?



I'd just do as feyd recommended. 8)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

yeah, but that doesn't work if the code includes htmlentities, and also doesn't solve the nl2br problem ><
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes it does...

the html entity is either converted to &pound; or &pound; already by the running of htmlentities... you're brining it back to what was actually pasted.

nl2br can be handled in a few ways...
  1. run it AFTER all processing tags
  2. run it before processing tags, then strip the creation from the php tags
  3. don't run it at all. If done correctly, from what I remember, nl2br isn't needed. Granted the browser needs to support a few CSS things then..
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

oh, I didn't know that, thanks :)

i'll continue with the nl2br things tomorrow, thanks for your answers.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

NEW QUESTION: Is there a way to replace all newlines with linebreaks (nl2br) everything outside [ php ], [/ php ] and [ code ], [/ code ]?

Here's the highligh-class:

Code: Select all

&lt;?php

class highlighter {
	var $code; // code to highlight
	var $adjust; // add &lt;?php if needed?
	var $line_numbers; // show linenumbers?
	var $highlight_method; // highlight_string / highlight_file
	var $title; // title for code
	var $isphp; // php or other code?
	var $linebreaks; // show linebreaks?

############## HIGHLIGHTER - initiation
function highlighter ($br = false, $method = 'highlight_string') {
	$this-&gt;linebreaks = $br;
	$this-&gt;highlight_method = $method;
	// set defaults
	$this-&gt;adjust = true;
	$this-&gt;line_numbers = true;
	$this-&gt;title = "PHP Code";
	$this-&gt;isphp = true;
}

############## SET_CONFIG - set configuration
function set_config ($property, $value) {
	if (isset ($this-&gt;$property)) {
		$this-&gt;$property = $value;
		// if $isphp is changed
		if ($property == 'isphp' &amp;&amp; $value == false) $this-&gt;title = 'Code';
		return true;
	} else {
		return false;
	}
}

############## SET_CODE - sets the code to highlight
function set ($code) {
	$this-&gt;code = $code; // decode special characters into real code
}

############## CLEAR - empty code
function clear () {
	$this-&gt;code = '';
}

############## MAKE_CSS - create html/css code
function make_css ($code) {

	if (PHP_VERSION &gt;= 5) { // PHP 5 highlighting
	$code = preg_replace (
	'{(&#1111;\w_]+)(\s*&lt;/span&gt;)'.
	'(\s*&lt;span\s+style="color: '.ini_get ('highlight.keyword').'"&gt;\s*\()}m',
	"&lt;a class='function' href='http://www.php.net/$1'&gt;$1&lt;/a&gt;$2$3",
	$code);
	} else { // old highlighting
	$code = preg_replace (
	'{(&#1111;\w_]+)(\s*&lt;/font&gt;)'.
	'(\s*&lt;font\s+color="'.ini_get ('highlight.keyword').'"&gt;\s*\()}m',
	"&lt;a class='function' href='http://www.php.net/$1'&gt;$1&lt;/a&gt;$2$3",
	$code);
	}

	$colors&#1111;ini_get ('highlight.bg')] = 'background';
	$colors&#1111;ini_get ('highlight.comment')] = 'comment';
	$colors&#1111;ini_get ('highlight.default')] = 'default';
	$colors&#1111;ini_get ('highlight.html')] = 'html';
	$colors&#1111;ini_get ('highlight.keyword')] = 'keyword';
	$colors&#1111;ini_get ('highlight.string')] = 'string';

	foreach ($colors as $color=&gt;$class) {
		$code = str_replace ('&lt;span style="color: '.$color.'"&gt;', "&lt;span class='$class'&gt;", $code); // PHP 5 highlighting
		$code = str_replace ('&lt;font color="'.$color.'"&gt;', "&lt;span class='$class'&gt;", $code); // old highlighting
	}

	return str_replace ('&lt;/font&gt;', '&lt;/span&gt;', $code);
}

############## PROCESS - processes code and creates output
function process () {

$code = $this-&gt;code;

$hlfunc = $this-&gt;highlight_method;

if ($this-&gt;isphp) { // highlight if php

if ($this-&gt;adjust &amp;&amp; !strstr ($code, '&lt;?php') &amp;&amp; !strstr ($code, '&lt;?')) $code = "&lt;?php
$code
?&gt;";
$code = $hlfunc ($code, true);
$code = $this-&gt;make_css ($code);
$lines = count (explode ('&lt;br /&gt;', $code));
if (!$this-&gt;linebreaks) $code = str_replace ('&lt;br /&gt;', '', $code); // delete linebreaks if requested
$output = @file_get_contents ('tpl/php.tpl');

} else { // not php
	$code = "&lt;span class='keyword'&gt;".nl2br (htmlentities ($code))."&lt;/span&gt;";
	$code = str_replace ('  ', ' &amp;nbsp;', $code); // 2 spaces &gt; 1 space + 1 nbsp
	$code = str_replace (chr(9), '&amp;nbsp; &amp;nbsp; ', $code); // Tab &gt; 4 spaces
	$lines = count (explode ('&lt;br /&gt;', $code));
	if (!$this-&gt;linebreaks) $code = str_replace ('&lt;br /&gt;', '', $code); // delete linebreaks if requested
	$output = @file_get_contents ('tpl/code.tpl');
}

for ($i = 1; $i &lt; ($lines + 1); $i++) {
	if($this-&gt;line_numbers) {
		$rownumbers .= "$i&lt;br /&gt;";
	}
}

$output = str_replace ('{%title%}', $this-&gt;title, $output); // title
$output = str_replace ('{%rownumbers%}', $rownumbers, $output); // rownumbers
$output = str_replace ('{%code%}', $code, $output); // actual code
$output = str_replace ('{%numlines%}', $lines, $output); // number of lines

return $output;
}

}
Formatting-function:

Code: Select all

&lt;?php

########## function format ($string, $level, $ add linebreaks) ##########
function format ($string, $lvl = 5, $br = false) {

$string = stripslashes ($string);

########## change html to entities if level is under 4
if ($lvl &lt; 4) $string = htmlentities ($string);
########## completly stip html tags if level is under 3
if ($lvl &lt; 3) $string = strip_tags ($string);

########## no tags or formatting allowed if level is under 2
if ($lvl &lt; 2) {
	$string = str_replace (chr(91), '&#91;', $string); // &#1111;
	$string = str_replace (chr(93), '&#93;', $string); // ]
	if ($br) $string = nl2br ($string); // replace newlines with linebreaks
	return $string; // skip formatting
}

if ((strpos ($string, '&#1111;') === FALSE) || (strpos ($string, ']') === FALSE)) { // if no code-blocks was found 
	return $string; // skip formatting
} // if code-blocks was found

######## code ########
// function which highlights code and returns formatted output
if (!function_exists ('highlight')) { // declare functions if they doesn't already exist

function highlight ($code) {
	@include_once ('mods/highlight.php');
	$hlight = New highlighter(!$br);
	$hlight-&gt;set ($code&#1111;2]);
	return $hlight-&gt;process ();
}
function highlight_title ($code) {
	@include_once ('mods/highlight.php');
	$hlight = New highlighter(!$br);
	$hlight-&gt;set_config ('title', $code&#1111;1]);
	$hlight-&gt;set ($code&#1111;3]);
	return $hlight-&gt;process ();
}
function hl_code ($code) {
	@include_once ('mods/highlight.php');
	$hlight = New highlighter(!$br);
	$hlight-&gt;set_config ('isphp', false);
	$hlight-&gt;set ($code&#1111;2]);
	return $hlight-&gt;process ();
}
function hl_code_title ($code) {
	@include_once ('mods/highlight.php');
	$hlight = New highlighter(!$br);
	$hlight-&gt;set_config ('isphp', false);
	$hlight-&gt;set_config ('title', $code&#1111;1]);
	$hlight-&gt;set ($code&#1111;3]);
	return $hlight-&gt;process ();
}

} // declare functions if they doesn't already exist

// php highlighting
$string = preg_replace_callback ("#\&#1111; php=(.*?)\](\s*)(.*?)(\s*)\&#1111;/php\]#si", "highlight_title", $string); ######### note ########### added a space due to phpBB bug ( forget this line) #########
$string = preg_replace_callback ("#\&#1111;php\](\s*)(.*?)(\s*)\&#1111;/php\]#si", "highlight", $string);
// code highlighting
$string = preg_replace_callback ("#\&#1111;code=(.*?)\](\s*)(.*?)(\s*)\&#1111;/code\]#si", "hl_code_title", $string);
$string = preg_replace_callback ("#\&#1111;code\](\s*)(.*?)(\s*)\&#1111;/code\]#si", "hl_code", $string);
####### /code ########

################### /replace ####################

if ($br) $string = nl2br ($string); // replace newlines with linebreaks 

### return formatted string
return $string;

} ######## /function format ($string, $level) ##########

?&gt;
the string sent is formatted with these arguments:

Code: Select all

$post&#1111;'body'] = format ($post&#1111;'body'], 3, true);
Example of string sent:

Code: Select all

&lt;b&gt;html coding - bold&lt;/b&gt;
&lt;a href='&#123;%id%&#125;'&gt;test&lt;/a&gt;
&#1111;b]testing....&#1111;/b]
&#1111;url=http://mgs3.vigge.net]url to mgs3-core&#1111;/url]
&#1111;php]
&lt;?php
echo "testing";
?&gt;
&#1111;/php]
The code.tpl file contains:

Code: Select all

&lt;div class='box'&gt;
&lt;div class='header'&gt;{%title%} ({%numlines%} lines) ::&lt;/div&gt;
&lt;div class='num'&gt;&lt;code&gt;
{%rownumbers%}
&lt;/code&gt;&lt;/div&gt;
&lt;div class='php'&gt;
{%code%}
&lt;/div&gt;
&lt;/div&gt;
Currently ouputs:

Code: Select all

&lt;b&gt;html coding - bold&lt;/b&gt;&lt;br /&gt;
&lt;a href='&#123;%id%&#125;'&gt;test&lt;/a&gt;&lt;br /&gt;

&lt;b&gt;testing....&lt;/b&gt;&lt;br /&gt;
&lt;a href='http://mgs3.vigge.net' rel='ext'&gt;url to mgs3-core&lt;/a&gt;&lt;br /&gt;
&lt;div class='box'&gt;&lt;br /&gt;
&lt;div class='header'&gt;PHP Code (3 lines) ::&lt;/div&gt;&lt;br /&gt;
&lt;div class='num'&gt;&lt;code&gt;&lt;br /&gt;
1&lt;br /&gt;2&lt;br /&gt;3&lt;br /&gt;&lt;br /&gt;
&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class='php'&gt;&lt;br /&gt;
&lt;code&gt;&lt;span class='html'&gt;&lt;br /&gt;
&lt;span class='default'&gt;&lt;?php&lt;br /&gt;

&lt;br /&gt;&lt;/span&gt;&lt;span class='keyword'&gt;echo &lt;/span&gt;&lt;span class='string'&gt;"testing"&lt;/span&gt;&lt;span class='keyword'&gt;;&lt;br /&gt;
&lt;br /&gt;&lt;/span&gt;&lt;span class='default'&gt;?&gt;&lt;/span&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
As you can see, alot of <br />s are added where they shouldn't.

If you want a live example, check http://vigge.ath.cx/p/board/ out.

Phew, lots of copying and pasting, I hope you understand what i've posted.

Thanks in advance.

EDIT: feel free to try the posting out, if you need any example output, etc.
(I will delete the entries when the board's gonna be available to the public anyway ;) )
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

updated question and latest reply...
still looking for a solution, I haven't been able to find anything working.
I tried using "white-space: pre;" in the css for the box, but that just made the codebox crazy :(
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

i think i've got a solution, but if it's gonna work, i need to know if and how you can create a RegExp pattern which doesn't match things inside <code> and </code> tags. Is this possible? I've searched google and php.net for any info on thi,s but I couldn't find anything.

Thanks in advance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it'd be better to just process out the nl2br junk inside the bbtag processor.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

the problem with that is that the output of the highlighting, ie, the divs and such, also fills up with linebreaks, and I haven't found a solution for it :(
Post Reply