Escape HTML code inside a <code> tag

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

Post Reply
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Escape HTML code inside a <code> tag

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Escape HTML code inside a <code> tag

Post by social_experiment »

Why not use htmlentities() ?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Escape HTML code inside a <code> tag

Post 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);
?>
Last edited by TildeHash on Thu Aug 25, 2011 4:30 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Escape HTML code inside a <code> tag

Post 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?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Escape HTML code inside a <code> tag

Post 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";
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Escape HTML code inside a <code> tag

Post 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?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Escape HTML code inside a <code> tag

Post 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";
?>
Post Reply