# PHP: do not interpret HTML surrounded by DIV

**URL:** <https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771>\
**Category:** programming\
**Created:** [June 9, 2008, 7:07pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771 "2008-06-09T19:07:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [June 9, 2008, 7:07pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/1 "2008-06-09T19:07:02Z")

</div>

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

```php
$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?

---

<div class="post-metadata">

**Author:** ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)\
**Post date:** [June 9, 2008, 7:15pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/2 "2008-06-09T19:15:34Z")

</div>

```php

$output = htmlspecialchars($output);

```

---

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [June 9, 2008, 7:37pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/3 "2008-06-09T19:37:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [June 9, 2008, 7:48pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/4 "2008-06-09T19:48:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [June 9, 2008, 7:57pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/5 "2008-06-09T19:57:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [June 9, 2008, 7:57pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/6 "2008-06-09T19:57:51Z")

</div>

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:

```php

$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…

---

<div class="post-metadata">

**Author:** ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)\
**Post date:** [June 9, 2008, 8:18pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/7 "2008-06-09T20:18:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [June 9, 2008, 8:33pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/8 "2008-06-09T20:33:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [June 9, 2008, 8:36pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/9 "2008-06-09T20:36:52Z")

</div>

This is what I have…

```php
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…

---

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [June 9, 2008, 9:07pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/10 "2008-06-09T21:07:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)\
**Post date:** [June 9, 2008, 9:17pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/11 "2008-06-09T21:17:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [June 9, 2008, 9:23pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/12 "2008-06-09T21:23:15Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [June 9, 2008, 9:51pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/13 "2008-06-09T21:51:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [June 9, 2008, 9:58pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/14 "2008-06-09T21:58:25Z")

</div>

[http://us3.php.net/preg\_match](http://us3.php.net/preg_match)

---

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [June 9, 2008, 10:01pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/15 "2008-06-09T22:01:22Z")

</div>

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\>

---

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [June 9, 2008, 10:19pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/16 "2008-06-09T22:19:59Z")

</div>

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  
}

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [June 9, 2008, 10:29pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/17 "2008-06-09T22:29:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [June 9, 2008, 10:47pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/18 "2008-06-09T22:47:45Z")

</div>

[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?

---

<div class="post-metadata">

**Author:** ![icio](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/icio/32/484_2.png) [@icio](https://forum.kirupa.com/u/icio)\
**Post date:** [June 10, 2008, 1:44pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/19 "2008-06-10T13:44:44Z")

</div>

This oughtta do the trick for you:

```php
<?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.

---

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [June 10, 2008, 9:22pm UTC](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771/20 "2008-06-10T21:22:23Z")

</div>

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](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:

```php

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

```php

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.

[Next page](https://forum.kirupa.com/t/php-do-not-interpret-html-surrounded-by-div/262771.md?page=2)
