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
TildeHash
Forum Commoner
Posts: 43 Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California
Post
by TildeHash » Tue Aug 23, 2011 6:53 am
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?
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Tue Aug 23, 2011 7:11 am
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
TildeHash
Forum Commoner
Posts: 43 Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California
Post
by TildeHash » Thu Aug 25, 2011 2:50 am
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.
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Thu Aug 25, 2011 3:00 am
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
TildeHash
Forum Commoner
Posts: 43 Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California
Post
by TildeHash » Thu Aug 25, 2011 4:29 am
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";
?>
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Thu Aug 25, 2011 6:42 am
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
TildeHash
Forum Commoner
Posts: 43 Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California
Post
by TildeHash » Thu Aug 25, 2011 6:48 am
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";
?>