【问题标题】:PHP get whole string not individual params in URLPHP获取整个字符串而不是URL中的单个参数
【发布时间】:2019-04-18 03:53:31
【问题描述】:

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

我希望能够获取附加在 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

【问题讨论】:

  • str_replace('www.example.com/w/', '', $url);

标签: php codeigniter get


【解决方案1】:

你只需要使用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;

输出

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

阅读更多关于str_replace()

【讨论】:

    【解决方案2】:

    试试这个,strpossubstr

    $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;
    

    输出:

    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() 将为您提供/w/ 的第一次出现,然后您可以使用+3 执行substr 以删除/w/

    或者试试这个,strstrstr_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;
    

    输出:

    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() 将为您提供给定 /w/ 的子字符串,并使用 str_replace() 从新字符串中删除 /w/

    【讨论】:

      猜你喜欢
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2012-01-08
      • 2019-06-20
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      相关资源
      最近更新 更多