Last active
November 11, 2020 21:19
-
-
Save hallindavid/3ca394505124406d353251728f4a2274 to your computer and use it in GitHub Desktop.
MySQL function to HTMLDecode a text value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE FUNCTION `HTMLDecode`(x TEXT) RETURNS text CHARSET latin1 | |
BEGIN | |
DECLARE TextString TEXT; | |
SET TextString = x; | |
SET TextString = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TextString,"<strong>", ""),"</strong>", ""),"<p>", ""),"</p>", "\n"),"<ul>", ""),"<li>", "- "),"</li>", "\n"),"</ul>", ""),"<br />", "\n"),"<br>", "\n"),"&", "&")," ", " "),"'", "'"),"<", "<"),">", ">"),"–", "-"),"•", "- "),"’", "'"),"…", "..."),"·", "- "),"”", "'"),"“", "'"),"‘", "'"),"#", "#"),"½", "1/2"),"°", "°"),"<i>", ""),"</i>", ""),""",'"'),"'",'"') | |
RETURN TextString; | |
END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$replacements = [ | |
"<strong>" => "", | |
"</strong>" => "", | |
"<p>" => "", | |
"</p>" => "\\n", | |
"<ul>" => "", | |
"<li>" => "- ", | |
"</li>" => "\\n", | |
"</ul>" => "", | |
"<br />" => "\\n", | |
"<br>" => "\\n", | |
"&" => "&", | |
" " => " ", | |
"'" => "'", | |
"<" => "<", | |
">" => ">", | |
"–" => "-", | |
"•" => "- ", | |
"’" => "'", | |
"…"=>"...", | |
"·"=>"- ", | |
"”"=>"'", | |
"“"=>"'", | |
"‘"=>"'", | |
"#"=>"#", | |
"½"=>"1/2", | |
"°"=>"°", | |
"<i>"=>"", | |
"</i>"=>"", | |
]; | |
$level = 1; | |
$rep_str = "TextString"; | |
foreach ($replacements as $a => $b) { | |
$rep_str = "REPLACE(" . $rep_str . ',"' . $a . '", "' . $b . '")'; | |
} | |
$rep_str = "REPLACE(" . $rep_str . ',""",\'"\')'; | |
$rep_str = "REPLACE(" . $rep_str . ',"'",\'"\')'; | |
echo "CREATE FUNCTION `HTMLDecode`(x TEXT) RETURNS text CHARSET latin1 \n"; | |
echo "BEGIN\n\n"; | |
echo "DECLARE TextString TEXT;\n"; | |
echo "SET TextString = x; \n\n"; | |
echo "SET TextString = " . $rep_str . "\n\n"; | |
echo "RETURN TextString; \n\n"; | |
echo "END"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use Tinkerwell (a Laravel tool to execute PHP code really well) (https://tinkerwell.app/)
I found myself losing track of all the
REPLACE
tags, so in order to better organize the replacements, I threw them into a php array and then created the create function statement dynamically. basically theTinkerCode.php
file is what you need to execute in PHP to change up your replacements/add/remove/edit etc...I have to give credit to Charlie Armor (here: https://forums.mysql.com/read.php?98,246527,246527 ) for the original code. I found that doing all the replace statements without testing if the string contents exist in the string first had much better performance.