Php 删除特定字符最后一个实例之后的所有字符
在最后一次出现斜杠字符Php 删除特定字符最后一个实例之后的所有字符,php,string,substring,Php,String,Substring,在最后一次出现斜杠字符/之后,如何删除字符串中的所有内容 例如,字符串为: http://localhost/new-123-rugby/competition.php?croncode=12345678 我想删除最后一个/之后的所有内容,以便它只显示: http://localhost/new-123-rugby/ 但是/后面的内容可以是可变长度的 请注意,URL中可能有任意数量的斜杠。它需要能够删除最后一个斜杠后的内容。可能不止上述示例所示。NOTA: 该问题经过编辑,因此原始答案(编辑
/
之后,如何删除字符串中的所有内容
例如,字符串为:
http://localhost/new-123-rugby/competition.php?croncode=12345678
我想删除最后一个/
之后的所有内容,以便它只显示:
http://localhost/new-123-rugby/
但是/
后面的内容可以是可变长度的
请注意,URL中可能有任意数量的斜杠。它需要能够删除最后一个斜杠后的内容。可能不止上述示例所示。NOTA:
该问题经过编辑,因此原始答案(编辑下方)与OP的要求不匹配。它未标记为OP的编辑
编辑: 更新了我的答案,因此它现在符合OP的要求: (现在它可以使用任意数量的斜杠) 也可以使用
http://localhost/new-123-rugby////////competition.php?croncode=12345678
<?php
$url = "http://localhost/new-123-rugby/competition.php?croncode=12345678";
echo dirname($url) . "/";
?>
原始答复: 这应该适合您:
<?php
$string = "http://localhost/new-123-rugby/competition.php?croncode=12345678";
echo $string = substr($string, 0, strpos(strrev($string), "/")-2);
?>
演示:你可以试试这个
$url = "http://localhost/new-123-rugby/competition.php?croncode=12345678";
preg_match("/[^\/]+$/", $url, $matches);
$newUrl = str_replace($matches[0],'',$url);
echo $newUrl;
解决方案1,使用substr()
+strrpos()
:
函数查找字符串中上次出现的/
的位置,提取所需的子字符串
缺点:如果$string
不包含“/”
,strrpos()
返回FALSE
,substr()
不返回我们想要的内容。需要先检查strrpos()
返回的值
解决方案2,使用explode()
+内爆()
:
或者,我们可以将最后一个组件设置为空,而不是array\u pop($array)
,无需在末尾添加额外的'/'
:
$string = 'http://localhost/new-123-rugby/competition.php?croncode=12345678';
$array = explode('/', $string);
if (count($array) > 1) {
$array[count($array) - 1] = ''; // empty the last component
echo(implode('/', $array)); // join the pieces back
}
缺点(两种版本):如果$string
不包含'/'
,explode()
生成一个包含单个值的数组,其余代码生成'/'
(第一段代码)或空字符串(第二段)。需要检查explode()
生成的数组中的项数
解决方案3,使用preg\u replace()
:
缺点:无。当
$string
包含'/'
且不包含'/'
时(在本例中,它不会修改$string
。是URL
字符串还是您想使用.htacess
?@Rizier123正如文章中所说,URI是字符串。它是$link,您可以使用explode(“/”,$str)
将为您提供array@Alex"解决办法简明扼要,。您应该将其作为答案发布。引用:它不起作用,因为使用了值2。可以有任意数量的斜杠。因此,我要求提供“最后一个”实例。最后一个例子可能是3个斜杠,8100…@Francesca如果你的问题中有来自start@Rizier123您提供的代码实际上甚至不能与上面的内容一起工作。例如,我刚刚测试了它,它在单词“competition”(竞争)的中途被打断了。所以它无论如何都不起作用。按照规定,最后一个/后面的内容可以是可变长度的。@Francesca现在更新了我的答案,所以它正是您想要的,我让它尽可能简单!另外,请将问题中的编辑标记为“编辑”。!错了!正如@Francesca在评论中所说:可以有任意数量的斜杠
,OP需要以下输出:http://localhost/new-123-rugby/
,但如果有多个斜杠,则无法得到OP想要的输出!请看:正如您所看到的,输出不是OP想要的@Rizier123,别介意,再读一遍。OP提到她想“删除最后一个斜杠后的内容”
$url = "http://localhost/new-123-rugby/competition.php?croncode=12345678";
preg_match("/[^\/]+$/", $url, $matches);
$newUrl = str_replace($matches[0],'',$url);
echo $newUrl;
$string = 'http://localhost/new-123-rugby/competition.php?croncode=12345678';
$pos = strrpos($string, '/');
if ($pos !== FALSE) {
echo(substr($string, 0, $pos + 1));
}
$string = 'http://localhost/new-123-rugby/competition.php?croncode=12345678';
$array = explode('/', $string);
if (count($array) > 1) {
array_pop($array); // ignore the returned value, we don't need it
echo(implode('/', $array).'/'); // join the pieces back, add the last '/'
}
$string = 'http://localhost/new-123-rugby/competition.php?croncode=12345678';
$array = explode('/', $string);
if (count($array) > 1) {
$array[count($array) - 1] = ''; // empty the last component
echo(implode('/', $array)); // join the pieces back
}
$string = 'http://localhost/new-123-rugby/competition.php?croncode=12345678';
echo(preg_replace('#/[^/]*$#', '/', $string));