PHP: do not interpret HTML surrounded by DIV

I’m writing a PHP tutorial. Of course, when I echo HTML tags they get interpreted by the browser, so I do this:

$output .= str_replace('<', '&lt', $output);
$output .= str_replace('>', '&gt', $output);

But then, at $output I have this <div class=“example”>, so I DO NOT WANT html to get interpreted only if is surrounded by the div class=“example”. All other HTML tags outsite the div class=“example” should be interpreted.

Suggestions?


$output = htmlspecialchars($output);

How would I apply this to only what’s surrounded by <div class=“example”> </div> ?

There is this column at the ARTICLES table: CONTENT. That’s where I have the articles text. So, where there is something that’s supposed to be code I surround it by <div class=“example”>

After reading the GeSHi documentation, looks like it’s going to be more complicated than I thought. How could I accept data from ONE column and then have the code know what is the PHP code and what is just text?

I guess I will sorrund the code with something like {CODE}.

then $row[‘content’] data into an array and explode it and make everything surrounded by the {CODE}{/CODE} work with GeSHI, what do you think?

I’m searching for a custom tags article but haven’t found anything useful yet. I guess a custom tag system will allow me to do what I want to accomplish.

I see what you are saying, but if there is another div within the example div, it gets very tricky, because the only way to know when to start interpreting html again is when a closing div tag is encountered. So if you have a nested div, the closing div tag within the example div will cause the script to start interpreting html again. I think that the only way to do it would be to create a custom tag, such as <nointerpret></nointerpret> to tell the script to start the htmlspecialchars or to turn it off.
Maybe something like:



$output = "FOOBARE <nointerpret>Foo Bar Baz Faz</nointerpret> BOoFar";
preg_match_all("/<nointerpret>[\/\(\)-:<>\w\s]+<\/nointerpret>/",$output,$noInterpret);
print_r($noInterpret); 


I think that should give you an array with the contents of the nointerpret tag. Maybe someone better at Regex could expand on this…

can always download vBulletin and scrape their logic :stuck_out_tongue: :lol:

hey! why did you deleted the set_header_type(GESHI_HEADER_PRE) ??

I could already parse with GESHI! woohooo! thanks!!!

I’m trying the PRE thing so it will only parse whats in between the pre tag. why did you deleted it?

This is what I have…

elseif($key=='content')
{
include_once 'geshi.php';
$source = $row[$value];
$language = 'php';
$geshi = new GeSHi($source, $language);
$geshi->set_header_type(GESHI_HEADER_PRE);
$return .= $geshi->parse_code();
}

This way it parses everything. I just want it to parse what’s inside the pre tags. I already set the header type and it doesn’t work. Im reading the documentation to find out how to correct it.

This is what’s inside $row[$value]…

If you are reading this tutorial, I assume you already know how to make a MySQL database connection from PHP. In case you do not know how, here is the code…

<pre>//////////////////////////////////////////
//// MySQL Database Connection ///////////
//////////////////////////////////////////
$host = “yourhost”;
$user = “yourusername”;
$db_name= “yourdatabasename”;
$pass= “yourpassword”;

$conn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db_name, $conn) or die(mysql_error());
</pre>

First, you need to obtain the page number you are in. If the user hasn’t clicked in any of the page links then it will bring them to page one. Notice that the variable $pageno will only contain numeric characters assigned to it. In case someone is playing around with the URL or trying an injection attack, the code will exit and the code won’t be executed…

ok, looks like I misunderstood the use of the set_header_type.

I thought that the only text that will be parsed was what was enclosed in the PRE tags. but then I looked at my source code and I realized that GESHI will parse everything, it will just use PRE tags for the coding.

im currently reading the documentation to find out if there is a way I can exclude parts of the text to be parsed by using a special tag.

any help will be welcomed.

Nah when I mentioned it I mentioned it w/ the wrong functionality cause I misread what it said. What that does is when you GeSHi something it wraps it in a pre or div by default. But that doesn’t solve your problem of how to determine what needs to be wrapped in that.

[QUOTE=simplistik;2339677]Nah when I mentioned it I mentioned it w/ the wrong functionality cause I misread what it said. What that does is when you GeSHi something it wraps it in a pre or div by default. But that doesn’t solve your problem of how to determine what needs to be wrapped in that.[/QUOTE]

Yes you’re right. Im trying to see if I can do it with explode.

ok, almost there. what is the PHP function to search for a string and if found it returns true?

I tried strstr and ereg but doesnt do what i want

http://us3.php.net/preg_match

I need a clue here. Let say I have something like…

blah blah blah blah
<geshi> code goes here </geshi>
blah blha blha blah
<geshi>more code here </geshi>

how would I get the chunks of plain text and the code in between the geshi tags in an array? I used explode, like this:

$explosion = explode(’<geshi>’, $data);

but it sets the chunks like this:

ARRAY ITEM 1
blah blah blah blah
<geshi>

ARRAY ITEM 2
code goes here </geshi>
blah blha blha blah
<geshi>

ARRAY ITEM 3
more code here </geshi>

I just need to separate what is text and what is code, so then I can apply the geshi to only the code.

I need to explode it in an array. check if there is a geshi tag and then apply geshi.

something like…

if(strstr($array[4], ‘<geshi>’))
{
geshi code
}
else
{
just echo because is a regular paragraph
}

Like I said above, you want to use preg_match
You can set up a regular expression that will get everything between the two tags. The tags I used in the example above are <nointerpret>, but you can change them to whatever.

[QUOTE=djheru;2339719]Like I said above, you want to use preg_match
You can set up a regular expression that will get everything between the two tags. The tags I used in the example above are <nointerpret>, but you can change them to whatever.[/QUOTE]

Yes, you’re right. But I want to also include what is not in between <geshi> tags in the array.

something like…
$array[0] = ‘text, plain text, this is a paragraph’
$array[1] = ‘<geshi>oh now this is code</geshi>’
$array[2] = ‘more paragraphs’;

im looking at split. you think i can do that with preg_match?

This oughtta do the trick for you:

<?php
    
    /**
     * A tag function -- change the input to uppercase
     * @param string $input The input text to change to uppercase
     */
    function tag_b($input) {
        $input = callback_prepare('b', $input);
        return '<strong>'.$input.'</strong>';
    }
    
    /**
     * A tag function -- highlight the PHP code
     * @param string $input The input text to highlight
     */
    function tag_php($input) {
        $input = callback_prepare('php', $input);
        return highlight_string($input, TRUE);
    }
    
    /**
     * Apply the formatting for each tag ($tags key) instance defined by the callback function ($tags value) over $text
     * @param array $tags   The tags and their respective callback functions
     * @param string $text  The text over which to search for tags
     */
    function apply_tags($tags, $text) {
        foreach ($tags as $tag => $func) $text = preg_replace_callback("%\\[$tag\\].*?\\[/$tag\\]%s", $func, $text);
        return $text;
    }
    
    /**
     * A helper function to the apply_tags callbacks -- sort the array inputs and remove the beginning and lead tags
     * @param string $tag   The name of the tag over which the calling function is applied
     * @param string $text  The text that was given to the callback function
     */
    function callback_prepare($tag, $text) {
        if (is_array($text)) $text = $text[0];
        if (substr($text, 0, strlen($tag)+2) == "[$tag]") $text = substr($text, strlen($tag)+2);
        if (substr($text, -strlen($tag)-3) == "[/$tag]") $text = substr($text, 0, -strlen($tag)-3);
        return $text;
    }
    
    $tags = array(
        'b' => 'tag_b',
        'php' => 'tag_php',
    );
    $source = file_get_contents('source.html');
    $source = apply_tags($tags, $source);
?>

Check out the example in the attached ZIP file. Hope that helps :thumb:

Edit: Oops, I forgot to change the comments when I changed the tag function. Never mind.

Awesome! Simple awesome!

You code worked great!!! Here is a preview of the page I’m trying to display.
http://www.webdeveloperwannabe.com/indexTest.php?id=4&show=1&pageno=1&pagetype=MySQL

I know the website looks very simple and looks like a total beginner designed it, but right now I’m focusing on how to display the info. It will improve.

This is part of the code:


elseif($key=='content')
{
	/**
	 * A tag function -- change the input to uppercase
	 * @param string $input The input text to change to uppercase
	 */
	function tag_b($input) {
		$input = callback_prepare('b', $input);
		return '<strong>'.$input.'</strong>';
	}
				
	/**
	 * A tag function -- highlight the PHP code
	 * @param string $input The input text to highlight
	 */
	function tag_php($input) {
		$input = callback_prepare('php', $input);
		return highlight_string($input, TRUE);
	}
	
	/**
	 * Apply the formatting for each tag ($tags key) instance defined by the callback function ($tags value) over $text
	 * @param array $tags   The tags and their respective callback functions
	 * @param string $text  The text over which to search for tags
	 */
	function apply_tags($tags, $text) {
	foreach ($tags as $tag => $func) $text = preg_replace_callback("%\\[$tag\\].*?\\[/$tag\\]%s", $func, $text);
		return $text;
	}
				
	/**
	 * A helper function to the apply_tags callbacks -- sort the array inputs and remove the beginning and lead tags
	 * @param string $tag   The name of the tag over which the calling function is applied
	 * @param string $text  The text that was given to the callback function
	 */
	function callback_prepare($tag, $text) {
		if (is_array($text)) $text = $text[0];
		if (substr($text, 0, strlen($tag)+2) == "[$tag]") $text = substr($text, strlen($tag)+2);
		if (substr($text, -strlen($tag)-3) == "[/$tag]") $text = substr($text, 0, -strlen($tag)-3);
		return $text;
	}
				
	$tags = array(
		'b' => 'tag_b',
		'php' => 'tag_php',
	);

	$return .= apply_tags($tags, $row[$value]);
}

Then I do


return nl2br($return);
exit();

Everything comes out double spaced. Any idea why? I know the nl2br does display line breaks, but here everything is double spaced and if I remove the nl2br everything comes out with no line breaks.