首页 > 解决方案 > 从开始和结束字符串中删除字符

问题描述

我只想MYID从 URL 输出。到目前为止我做了什么:

$url = "https://whatever.expamle.com/display/MYID?out=1234567890?Browser=0?OS=1";
echo substr($url, 0, strpos($url, "?out="));

输出:https ://whatever.expamle.com/display/MYID

$url = preg_replace('#^https?://whatever.expamle.com/display/#', '', $url);
echo $url;

输出:MYID?out=1234567890?Browser=0?OS=1

我怎样才能结合这个?谢谢。

标签: phppreg-replacesubstrstrpos

解决方案


对于更通用的解决方案,我们可以使用正则表达式preg_match_all

$url = "https://whatever.expamle.com/display/MYID?out=1234567890?Browser=0?OS=1";
preg_match_all("/\/([^\/]+?)\?/", $url, $matches);
print_r($matches[1][0]);  // MYID

推荐阅读