Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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_Url_Preg Match - Fatal编程技术网

Php 如何删除URL的一部分

Php 如何删除URL的一部分,php,url,preg-match,Php,Url,Preg Match,假设我的URL是: http://example.com/forum/index.php?topic=53.msg251#msg251 在这一部分,我不知道如何删除: .msg251#msg251 我确实尝试过,但我不确定我是否已经接近正确的做法 $linkraw = $items->link; $linkpattern = '/^.msg247#msg247/'; $link = preg_match($linkpattern, $linkraw); 正确的做法是什么?我正在

假设我的URL是:

http://example.com/forum/index.php?topic=53.msg251#msg251
在这一部分,我不知道如何删除:

.msg251#msg251
我确实尝试过,但我不确定我是否已经接近正确的做法

 $linkraw = $items->link;
 $linkpattern = '/^.msg247#msg247/';
 $link = preg_match($linkpattern, $linkraw);
正确的做法是什么?我正在努力学习

“preg_match()返回模式匹配的次数。这将是 0次(不匹配)或1次,因为preg_match()将停止 搜索第一个匹配项。相反,preg_match_all()将 继续,直到到达主题的结尾。preg_match()返回 如果发生错误,则为FALSE。“

Preg_Match仅在字符串中存在模式时进行检查。它不会移除某些东西

可以使用str_replace()替换该零件

$link = str_replace(".msg251#msg251", "", $linkraw);
“preg_match()返回模式匹配的次数。这将是 0次(不匹配)或1次,因为preg_match()将停止 搜索第一个匹配项。相反,preg_match_all()将 继续,直到到达主题的结尾。preg_match()返回 如果发生错误,则为FALSE。“

Preg_Match仅在字符串中存在模式时进行检查。它不会移除某些东西

可以使用str_replace()替换该零件

$link = str_replace(".msg251#msg251", "", $linkraw);

如果要删除,请使用:


如果要删除,请使用:


使用
http\u build\u url
函数非常简单(需要PECL-PECL\u-http>=0.21.0)


使用
http\u build\u url
函数非常简单(需要PECL PECL\u http>=0.21.0)


字符串函数和足够用于此任务。而且速度肯定更快

 $link = substr($linkraw, 0, strrpos($linkraw, "."))
说明:
  • strrpos
    从字符串末尾查找
    的位置
  • substr
    提取子字符串,直到在上一步中找到
    的位置
  • 这什么时候行? 在
    http://example.com/forum/index.php?topic=53.msg251#msg251

    http://example.com/forum/index.php?topic=53.new#new

    但是不是
    http://example.com/forum/index.php?topic=53.msg251#msg251.new#new

    字符串函数和足够用于此任务。而且速度肯定更快

     $link = substr($linkraw, 0, strrpos($linkraw, "."))
    
    说明:
  • strrpos
    从字符串末尾查找
    的位置
  • substr
    提取子字符串,直到在上一步中找到
    的位置
  • 这什么时候行? 在
    http://example.com/forum/index.php?topic=53.msg251#msg251

    http://example.com/forum/index.php?topic=53.new#new


    但是不是
    http://example.com/forum/index.php?topic=53.msg251#msg251.new#new

    您可以使用匹配还是删除?字符串函数strrpos和substr足以完成此任务。查看我的答案。您可以使用匹配还是删除?字符串函数strrpos和substr足以完成此任务。看到我的答案了。你能解释一下应用这个时发生了什么吗?如果“.new#new”在URL的末尾,这难道不仍然有效吗?看看我的解释。它将找到最后一个
    ,然后在该点之前拆分字符串。因此,如果您添加任何具有
    的内容。它不会在新添加的
    处拆分。您能解释一下应用此项时会发生什么吗?如果“.new#new”位于URL的末尾,这是否仍然有效?请参阅我的解释。它将找到最后一个
    ,然后在该点之前拆分字符串。因此,如果您添加任何具有
    的内容。它不会在新添加的
    处拆分。