Regex帮助从url中获取值


Regex help grabbing values from urls

我正在努力创建一个正则表达式来打印我想要的url部分。使用PHP,我获取主域名之后的路径。它们看起来像:

this-link-1
this-is-another-one-3

我用来抓取这些文件的php如下:

$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$whereami =  parse_url($url, PHP_URL_PATH);
echo $whereami;

有人能帮我写一个正则表达式来删除-whereami变量的-#吗?因此,在这个例子中,$wherami在运行表达式

之后将是this-linkthis-is-another-one
preg_replace("/-'d+$/", "", $whereami)

示例:

echo preg_replace("/-'d+$/", "", "this-is-another-one-3");

输出:

this-is-another-one

您也可以在不使用regex:的情况下执行此操作

echo implode('-', explode('-', 'this-link-1', -1));

结果:

this-link