Saturday, May 8, 2010

url decode in MySQL

I got the following function from Garrett Hill. The following function is the same as urldecode($text) function in php. Garrett just saved my life.


--------------------------------------Function Starts--------------------------------

DELIMITER $$

DROP FUNCTION IF EXISTS `typing_wikipedia`.`url_decode`$$
CREATE FUNCTION `typing_wikipedia`.`url_decode`(original_text text) RETURNS text CHARSET latin1
BEGIN
declare new_text text default null;
declare pointer int default 1;

set new_text = replace(original_text,'+',' ');
while (LOCATE("%",new_text,pointer) <> 0 )&& (pointer < length(new_text)) do
set pointer = LOCATE("%",new_text,pointer);
set new_text = concat(left(new_text,pointer-1),char(conv(mid(new_text,pointer+1,2),16,10)),right(new_text,length(new_text)-(pointer+2)));
set pointer = pointer + 1;
end while;

return new_text;

END;

$$

DELIMITER ;

------------------------------------Function ends-------------------------------

Reference: MySQL Forge

No comments:

Post a Comment