How to fetch Domain names from Urls in MySql |
[eluser]FutureKing[/eluser]
My db table has a column called url which contains data like: http://xyz.com/cat/index/?page=2 http://www.google.com/webmaster.php http://yahoo.com/dsdsd/category.aspx etc. I want to just fetch their domain names like: xyz.com google.com yahoo.com How should I write the query in mysql to get the above result. Please help.
[eluser]PhilTem[/eluser]
I'm pretty sure it can't be done on the MySQL side because MySQL-queries only allow REGEXP for a WHERE part. You need to post-process the data in pure PHP after getting it from the database. This process can be done easily with preg_match
[eluser]FutureKing[/eluser]
Someone in Digital Point gave me the answer SELECT SUBSTRING_INDEX(REPLACE(REPLACE(url, "http://", ""), "www.", ""), '/', 1);
[eluser]PhilTem[/eluser]
Hm, true story. That's one way of doing it. But: Is it faster than post-processing it in PHP? Just curious about it
[eluser]FutureKing[/eluser]
[quote author="PhilTem" date="1346690161"]Hm, true story. That's one way of doing it. But: Is it faster than post-processing it in PHP? Just curious about it [/quote] I don't know. |
Welcome Guest, Not a member yet? Register Sign In |