Welcome Guest, Not a member yet? Register   Sign In
How to fetch Domain names from Urls in MySql
#1

[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.
#2

[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
#3

[eluser]FutureKing[/eluser]
Someone in Digital Point gave me the answer

SELECT SUBSTRING_INDEX(REPLACE(REPLACE(url, "http://", ""), "www.", ""), '/', 1);
#4

[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 Wink
#5

[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 Wink[/quote]

I don't know.




Theme © iAndrew 2016 - Forum software by © MyBB