Url decode using utf8

Developed In: SQL — Contributed by: Lukas Oberhuber

Does url_decode respecting utf8 characters. Loosely based on Garrett Hill's url_decode http://forge.mysql.com/tools/tool.php?id=93.


Lukas Oberhuber
SQL
  1. DELIMITER $$
  2.  
  3. DROP FUNCTION IF EXISTS `url_decode` $$
  4. CREATE DEFINER=`root`@`%` FUNCTION `url_decode`(original_text text) RETURNS text CHARSET utf8
  5. BEGIN
  6. declare new_text text DEFAULT NULL;
  7. declare pointer int DEFAULT 1;
  8. declare end_pointer int DEFAULT 1;
  9. declare encoded_text text DEFAULT NULL;
  10. declare result_text text DEFAULT NULL;
  11.  
  12. SET new_text = REPLACE(original_text,'+',' ');
  13.  
  14. SET pointer = LOCATE("%", new_text);
  15. while pointer <> 0 && pointer < (char_length(new_text) - 2) do
  16. SET end_pointer = pointer + 3;
  17. while mid(new_text, end_pointer, 1) = "%" do
  18. SET end_pointer = end_pointer+3;
  19. end while;
  20.  
  21. SET encoded_text = mid(new_text, pointer, end_pointer - pointer);
  22. SET result_text = convert(unhex(REPLACE(encoded_text, "%", "")) USING utf8);
  23. SET new_text = REPLACE(new_text, encoded_text, result_text);
  24. SET pointer = LOCATE("%", new_text, pointer + char_length(result_text));
  25. end while;
  26.  
  27. RETURN new_text;
  28.  
  29. END $$
  30.  
  31. DELIMITER ;

You must be logged in to tag this tool

No Comments yet

Votes

Not yet rated.
You must be logged in to vote.

Watches

0 members are watching this tool
You must be logged in to track this tool.

Provide Feedback

Please note:
HTML will be purified, but we allow for a number of HTML tags so that you have the flexibility to decorate your comment text to some extent. The comments allow the following HTML tags:

strong, b, em, blockquote, a, code, pre

To put code into your comment, simply encapsulate your code with
[code language="XXX"][/code], where XXX is any common language, for instance "PHP", "SQL", "C", etc.



You must be logged in to comment