簡體   English   中英

PHP獲取整個字符串,而不是URL中的單個參數

[英]PHP get whole string not individual params in URL

如果我有一個域名,例如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()

$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()的更多信息

嘗試使用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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM