Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正则表达式帮助从URL获取值_Php_Regex - Fatal编程技术网

Php 正则表达式帮助从URL获取值

Php 正则表达式帮助从URL获取值,php,regex,Php,Regex,我正在努力创建一个正则表达式来打印我想要的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['R

我正在努力创建一个正则表达式来打印我想要的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;
有人能帮我写一个正则表达式来删除wheremi变量的-#吗?因此,在这个例子中,$wheremi将是
这个链接
,而
这是另一个链接

preg_replace("/-\d+$/", "", $whereami)
例如:

echo preg_replace("/-\d+$/", "", "this-is-another-one-3");
输出:

this-is-another-one
例如:

echo preg_replace("/-\d+$/", "", "this-is-another-one-3");
输出:

this-is-another-one

您也可以在不使用正则表达式的情况下执行此操作:

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

this-link

您也可以在不使用正则表达式的情况下执行此操作:

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

this-link

对这很有用,所以我永远不会学正则表达式:)是的!这非常有用,我永远不会学习正则表达式:)