douxigai8757
2019-04-18 03:53
浏览 117
已采纳

PHP在URL中获取整个字符串而不是单个参数

If I have a domain e.g. www.example.com/w/

I want to be able to get the whole string of text appended after the URL, I know how to get parameters in format ?key=value, that's not what I'm looking for.

but I would like to get everything after the /w/ prefix, the whole string altogether so if someone appended after the above

www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html

I would be able to get https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html

I was thinking of installing codeigniter on my server if that helps, but at the moment I'm just using core php

图片转代码服务由CSDN问答提供 功能建议

如果我有一个域,例如 www.example.com/w/

nn

我希望能够在URL之后添加整个文本字符串,我知道如何获取格式参数?key = value,这是 不是我正在寻找的东西。

但是我希望得到/ w /前缀后的所有内容,整个字符串完全如此,如果有人追加上面的

www.example.com/w/ https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html

我能够 获取 https://www.nytimes.com /2019/04/17/us/politics/trump-mueller-report.html

我想在我的服务器上安装codeigniter,如果这有帮助,但此刻 我只是使用核心php

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drci47425 2019-04-18 04:45
    已采纳

    You just need to use str_replace()

    $str = "www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html";
    $str2 =  str_replace('www.example.com/w/', '', $str);
    echo $str2;
    

    Output

    https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html 
    

    Read more about str_replace()

    打赏 评论
  • douwen0647 2019-04-18 04:31

    Try this, with strpos and substr

    $str = "www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html";
    echo $str.'<pre>';
    $start =  strpos($str, '/w/');
    echo substr($str, $start + 3);die;
    

    Output:

    www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
    
    https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
    

    strpos() will give you first occurrence of /w/ and from there you can do substr with +3 to remove /w/

    OR Try this, with strstr and str_replace

    $str = "www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html";
    echo $str.'<pre>';
    $str1 =  strstr($str, '/w/');
    echo $str1.'<pre>';
    $str2 = str_replace('/w/', '', $str1);
    echo $str2.'<pre>';die;
    

    Output:

    www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
    
    /w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
    
    https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
    

    strstr() will give you substring with given /w/ and use str_replace() to remove /w/from new string

    打赏 评论

相关推荐 更多相似问题