给定一个 URL,我如何才能获得域? [英] Given a URL, how can I get just the domain?

查看:30
本文介绍了给定一个 URL,我如何才能获得域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定的 URL 如下:

Given URLs like:

http://online.wsj.com/
http://online.wsj.com/article/SB10001424052970204409004577158764211274708.html
http://www.techcrunch.com/2012/01/13/techcrunch-coo/

使用 Ruby/Rails,我如何只返回域?

Using Ruby/Rails, how can I return back just the domain?

online.wsj.com
online.wsj.com
techcrunch.com

没有协议,没有斜线,只有子域(如果不是www)、域和分机?

No protocol, no slashes, just the subdomain if it's not www, and the domain, and ext?

推荐答案

使用 Addressable::URI.parse 和 #host 实例方法:

Use Addressable::URI.parse and the #host instance method:

Addressable::URI.parse("http://techcrunch.com/foo/bar").host #=> "techcrunch.com" 

这篇关于给定一个 URL,我如何才能获得域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆