MySQL: Replacing URL Escape Sequences
So you want to store URLs in MySQL, and the URLs have those annoying %20%27%7C%26%5E%2B%2D%25 symbols? And you want to be able to show your users some kind of human-readable information. You might want to consider using this trick. Take this list of commonly escaped characters as an example:
%20 - space %27 - ' %7C - | %26 - & %5E - ^ %2B - + %2D - - %25 - %
So, how about we do some search’n’replace on that?
mysql> SET @url:='%20%27%7C%26%5E%2B%2D%25';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @url as original,
-> REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
-> REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
-> @test,'%20',' '),
-> '%27','\''),
-> '%7C','|'), -- REPLACE() is case sensitive
-> '%7c','|'), -- so we have
-> '%26','&'),
-> '%5E','^'),
-> '%5e','^'), -- to repeat
-> '%2D','-'),
-> '%2d','-'), -- ourselves
-> '%2B','+'),
-> '%2b','+'), -- sometimes
-> '%25','%') as replaced;
+--------------------------+----------+
| original | replaced |
+--------------------------+----------+
| %20%27%7C%26%5E%2B%2D%25 | '|&^+-% |
+--------------------------+----------+
1 row in set (0.01 sec)
mysql>
We can easily turn this into a function:
mysql> CREATE DATABASE foo;
Query OK, 1 row affected (0.03 sec)
mysql> USE foo;
Database changed
mysql> CREATE FUNCTION url_replace
-> (url VARCHAR(1024))
-> RETURNS VARCHAR(1024)
-> BEGIN
-> RETURN REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
-> REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
-> @test,'%20',' '),
-> '%27','\''),
-> '%7C','|'),
-> '%7c','|'),
-> '%26','&'),
-> '%5E','^'),
-> '%5e','^'),
-> '%2D','-'),
-> '%2d','-'),
-> '%2B','+'),
-> '%2b','+'),
-> '%25','%');
-> END$$
Query OK, 0 rows affected (0.02 sec)
mysql> DELIMITER ;
mysql> SELECT url_replace('%20%27%7C%26%5E%2B%2D%25') as bar;
+----------+
| bar |
+----------+
| '|&^+-% |
+----------+
1 row in set (0.00 sec)
mysql>
I hope this is useful to you guys. Cheers!
Useful links:
On this page
Share this
Share this
More resources
Learn more about Pythian by reading the following blogs and articles.
Best practice for setting up MySQL replication filters

Best practice for setting up MySQL replication filters
Apr 1, 2016 9:48:00 AM
7
min read
pt-online -schema corner case: When you don’t want to replicate your alter activities across the whole cluster.
pt-online -schema corner case: When you don’t want to replicate your alter activities across the whole cluster.
Nov 21, 2022 12:00:00 AM
5
min read
Setting up MySQL encrypted replication on MySQL 5.7 with GTID
Setting up MySQL encrypted replication on MySQL 5.7 with GTID
Jul 23, 2018 12:00:00 AM
8
min read
Ready to unlock value from your data?
With Pythian, you can accomplish your data transformation goals and more.