douzhi1919 2011-03-22 14:57
浏览 12
已采纳

无论格式如何,都删除URL

Having a brain freeze...

Have a URL which may be in any of the formats :

http://url.com/stuff
url.com/somestuff
www.url.com/otherstuff
https://www.url.com/morestuff

You get the picture.

How do I remove the .com part to leave just the various 'stuff' parts ? For example, the above would end up :

stuff
somestuff
otherstuff
morestuff
  • 写回答

6条回答 默认 最新

  • dph23577 2011-03-22 15:07
    关注

    This should do the trick for you!

    <?php
    $url = "http://url.com/stuff";
    
    $querystring = preg_replace('#^(https|http)?(://)?(www.)?([a-zA-Z0-9-]+)\.[a-zA-Z]{2,6}/#', "", $url);
    
    echo $querystring;
    

    I submitted this answer because I'm not very fond of solutions using explode() to handle this. Maybe your query string contains more slashes so, you'd have to write exceptions for those cases.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?