Page 1 of 1

Escape HTML code inside a <code> tag

Posted: Tue Aug 23, 2011 6:53 am
by TildeHash
Hello, I've been trying to find a way to escape HTML code inside a <code> tag.

Using eregi_replace, this is the code I'm trying to get to work:

Code: Select all

<?php
$code="This is a <code><b>super</b></code> test. Yeah!";
echo eregi_replace('<code>((.*)+)</code>', "<code>".str_replace(array("<", ">"), array("<", ">"), "\\1")."</code>", $code);
?>
It executes, but doesn't escape the HTML code. What am I doing wrong? Or is there a better way of achieving the same thing a different way?

Re: Escape HTML code inside a <code> tag

Posted: Tue Aug 23, 2011 7:11 am
by social_experiment
Why not use htmlentities() ?

Re: Escape HTML code inside a <code> tag

Posted: Thu Aug 25, 2011 2:50 am
by TildeHash
social_experiment wrote:Why not use htmlentities() ?
How? Even this code doesn't work:

Code: Select all

<?php
$code="This is a <code><b>super</b></code> test. Yeah!";
echo eregi_replace('<code>((.*)+)</code>', "<code>".htmlentities("\\1")."</code>", $code);
?>

Re: Escape HTML code inside a <code> tag

Posted: Thu Aug 25, 2011 3:00 am
by social_experiment
Ah. Didn't spot the htmlentities() in your expression. :|

Code: Select all

<?php
$code="This is a <code><b>super</b></code> test. Yeah!";
echo eregi_replace('<code>(.+)</code>', "<code>".htmlentities("\\1"))."</code>", $code);
?>
What about changing the regular expression?

Re: Escape HTML code inside a <code> tag

Posted: Thu Aug 25, 2011 4:29 am
by TildeHash
social_experiment wrote:Ah. Didn't spot the htmlentities() in your expression. :|

Code: Select all

<?php
$code="This is a <code><b>super</b></code> test. Yeah!";
echo eregi_replace('<code>(.+)</code>', "<code>".htmlentities("\\1")."</code>", $code);
?>
What about changing the regular expression?
I don't think it's a regular expression problem. The code grabs the content inside the <code> tag successfully.

Test this code to see what I'm talking about:

Code: Select all

<?php
$code="This is a <code><b>SupEr</b></code> test. Yeah!";

# Remove the <code> tags - works
echo eregi_replace('<code>(.+)</code>', "\\1", $code)."<br>\n";

# Place italic tags - works
echo eregi_replace('<code>(.+)</code>', "<code><i>\\1</i></code>", $code)."<br>\n";

# Escape - doesn't work
echo eregi_replace('<code>(.+)</code>', "<code>".htmlentities("\\1")."</code>", $code)."<br>\n";

# Make lower case - doesn't work
echo eregi_replace('<code>(.+)</code>', "<code>".strtolower("\\1")."</code>", $code)."<br>\n";

# Replace - doesn't works
echo eregi_replace('<code>(.+)</code>', "<code>".str_ireplace("sup", "", "\\1")."</code>", $code)."<br>\n";
?>

Re: Escape HTML code inside a <code> tag

Posted: Thu Aug 25, 2011 6:42 am
by social_experiment
Yeah the expression works either way, i've tested it right after i posted. What is the code for, to have actual code be displayed (like js or html) between code tags?

Re: Escape HTML code inside a <code> tag

Posted: Thu Aug 25, 2011 6:48 am
by TildeHash
social_experiment wrote:Yeah the expression works either way, i've tested it right after i posted. What is the code for, to have actual code be displayed (like js or html) between code tags?
Yes. To have it output "This is a <code><b>SupEr</b></code> test. Yeah!"

I got it too work for me by doing this:

Code: Select all

<?php
$code="This is a <cOdE><b>SupEr</b></CodE> test. Yeah!";
echo preg_replace('/(<code>)(.+)(<\/code>)/i e', "'\\1'.htmlentities('\\2').'\\3'", $code)."<br>\n";
?>