doupa2871 2014-08-05 18:19 采纳率: 100%
浏览 24
已采纳

PHP字符串拆分

I'm new to PHP. I was reading this: http://php.net/manual/en/ref.strings.php however I'm looking for additional clarification.

I know in python I can call .split("...") as well as in Java.

So I found chunk_split, str_split, and explode. What are the reasons for using each? From what I was reading I was under the impression that each could be used for the same task. Is there an explicit advantage for using one over the other?

I'm looking to split a url to get the "Base" (not sure what else to call it)

Ex:

http://www.stackoverflow.com/questions/ask

I was thinking I could cut off the front with if statements

if = http:// or http://www. or www. etc... #pseudo

Then cut by "/" chars and take the front.

Which function is best purposed for this?

  • 写回答

1条回答 默认 最新

  • dousha7904 2014-08-05 18:26
    关注

    The function you're after is parse_url():

    <?php
    
    $url = 'http://www.stackoverflow.com/questions/ask';
    
    $parsed_url = parse_url($url);
    
    echo '<pre>';
    print_r($parsed_url);
    echo '</pre>';
    

    output:

    Array
    (
        [scheme] => http
        [host] => www.stackoverflow.com
        [path] => /questions/ask
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测