FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Undo htmlspecialchars





DanielXP
I am currently running on PHP version 4.4.4.

I have my BBCodes script with a syntax highlighter but for this i need the htmlspecialchars to be undone.

What is the code/funtion for this to be done.

Thanks,
Daniel
MrBlueSky
Code:


$table = get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES);
$rev_trans = array_flip($table);
$original_string = strtr($str,$rev_trans); 
// $original_string now has special chars back to 'normal' ones



Use ENT_QUOTES or ENT_COMPAT instead of ENT_NOQUOTES depending on the second parameter of your call to htmlspecialchars().
DanielXP
Thanks MrBlueSky!

That code works but im trying to add it to my PHP BBCode and its not doing it.

Code:
<?php
function UndoHtml($str) {
$table = get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES);
$rev_trans = array_flip($table);
$str = strtr($str,$rev_trans);
return $str;
} //the remove htmlspecialchars which MyBlueSky gave me.
function bbcode($content){
$content = preg_replace("/\[php\](.*?)\[\/php\]/si", "&nbsp;&nbsp;&nbsp;<fieldset style='width: 450'><table width='100%' cellpadding='0' cellspacing='0' style='border-bottom:1px dashed #000000;'><tr><td><b>PHP:</b></td></tr></table>".highlight_string(UndoHtml('\\1'), true)."</fieldset>",$content);
return $content;
}
?>

It shows the code and that but it don't undo the htmlspecialchars.

As i said before i tested the script on its own and it works fine.

It to do with the '\\1' not changing the var $str as i can't.. Any way to get around this?

Thanks,
DanielXP
MrBlueSky
This should work:

Code:

<?php
   function UndoHtml($str) {
      $table = get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES);
      $rev_trans = array_flip($table);
      $str = strtr($str,$rev_trans);
      return $str;
   } //the remove htmlspecialchars which MyBlueSky gave me.

   function bbcode($content){
      $content = preg_replace_callback("/\[php\](.*?)\[\/php\]/si",'callback_bbcode' ,$content);
      return $content;
   }
   
   // This functions is called by bbcode, never call it directly
   function callback_bbcode($matches) {
       $str = "&nbsp;&nbsp;&nbsp;<fieldset style='width: 450'><table width='100%' cellpadding='0' cellspacing='0' style='border-bottom:1px dashed #000000;'><tr><td><b>PHP:</b></td></tr></table>";
       $str .= highlight_string(UndoHtml($matches[1]), true);
       $str .= "</fieldset>";
       return $str;
   }
   
   
?>
DanielXP
Works fine thanks!

But with my code im getting this,

<?<br />
echo &quot;This is a test.&quot;;<br />
exit;<br />
?>

where it should display

<?
echo "This is a test.";
exit;
?>
DanielXP
I have change my undo funtion to this.

Code:
function UndoHtml($str) {
$str = strtr($str, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
$str = str_replace ("<br />", "", $str);
return $str;
}


AND IT WORKS,

Thanks MrBlue for all ya help!
Related topics
req: PHP to list files in directory, and link to them
Windows Tips&tricks!
751 Useful Windows XP Files
secure your pc
My Laptop prblm
Trippling Your Download Speeds
PHP function(); &file=""?
got undo closetab?
25 short cuts in Windows Xp
error in my PHP script
need php help save command
htmlspecialchars
htmlentities and htmlspecialchars
Java undo
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.