gpt4 book ai didi

ruby-on-rails - 给定一个 URL,我怎样才能只得到域?

转载 作者:数据小太阳 更新时间:2023-10-29 06:40:04 24 4
gpt4 key购买 nike

给定的 URL 如下:

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

使用 Ruby/Rails,我怎样才能只返回域?

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

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

最佳答案

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

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

关于ruby-on-rails - 给定一个 URL,我怎样才能只得到域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8855236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com