So the test case string may be:
所以测试用例字符串可能是:
http://example.com/?u=ben
Or
要么
http://example.com
I'm trying to remove everything after the last occurrence of a '/' but only if it's not part of the 'http://'. Is this possible!?
我试图在最后一次'/'之后删除所有内容,但前提是它不是'http://'的一部分。这可能吗!?
I have this so far:
到目前为止我有这个:
$url = substr($url, 0, strpos( $url, '/'));
But does not work, strips off everything after first '/'.
但是不起作用,在第一个'/'之后去除所有东西。
4 个解决方案
#1
14
You should use the tool that is designed for this type of job, parse_url
您应该使用为此类作业而设计的工具parse_url
url.php
url.php
<?php
$urls = array('http://example.com/foo?u=ben',
'http://example.com/foo/bar/?u=ben',
'http://example.com/foo/bar/baz?u=ben',
'https://foo.example.com/foo/bar/baz?u=ben',
);
function clean_url($url) {
$parts = parse_url($url);
return $parts['scheme'] . '://' . $parts['host'] . $parts['path'];
}
foreach ($urls as $url) {
echo clean_url($url) . "\n";
}
Example:
例:
·> php url.php
http://example.com/foo
http://example.com/foo/bar/
http://example.com/foo/bar/baz
https://foo.example.com/foo/bar/baz
#2
64
You must use strrpos function not strpos ;-)
你必须使用strrpos函数而不是strpos ;-)
substr($url, 0, strrpos( $url, '/'));
#3
0
$cutoff = explode("char", $string);
echo $cutoff[0]; // 2 for what you want and 3 for the index
also
也
echo str_replace("http://", "", $str);
echo str_replace(“http://”,“”,$ str);
#4
0
Actually, a simpler solution to what you want to achieve is to play with a few string manipulation functions of PHP.
实际上,一个更简单的解决方案是你想要实现的是使用PHP的一些字符串操作函数。
First of all, you need to find the position of the last occurrence of the '/'. You can do this by using the strrpos() function (be careful, it is with 2 r);
首先,您需要找到最后一次出现'/'的位置。你可以使用strrpos()函数来做到这一点(小心,它是2 r);
Then, if you provide this position as a negative value, as a second parameter to the substr() function, it will start the search of the substring from the end.
然后,如果您将此位置作为负值提供,作为substr()函数的第二个参数,它将从末尾开始搜索子字符串。
The second problem is that you want as result the part of the strings that is at the LEFT of the last '/'. To achieve this, you will have to provide substr() with a negative value to the third parameter which will indicate how many characters you want to remove.
第二个问题是你想要的结果是最后一个'/'左边的字符串部分。要实现此目的,您必须为第三个参数提供带负值的substr(),这将指示要删除的字符数。
To be sure of how many parameters you need to remove, you will have to extract the string part at the RIGHT of the '/' first, and then count its length.
要确保需要删除多少个参数,首先必须在'/'的右边提取字符串部分,然后计算其长度。
//so given this url:
$current_url = 'http://example.com/firstslug/84'
//count how long is the part to be removed
$slug_tbr = substr($current_url, strrpos($current_url, '/')); // '/84'
$slug_length = strlen(slug_tbr); // (3)
/*get the final result by giving a negative value
to both second and third parameters of substr() */
$back_url = substr($current_url, -strrpos($current_url, '/'), -$slug_length);
//result will be http://example.com/firstslug/
#1
14
You should use the tool that is designed for this type of job, parse_url
您应该使用为此类作业而设计的工具parse_url
url.php
url.php
<?php
$urls = array('http://example.com/foo?u=ben',
'http://example.com/foo/bar/?u=ben',
'http://example.com/foo/bar/baz?u=ben',
'https://foo.example.com/foo/bar/baz?u=ben',
);
function clean_url($url) {
$parts = parse_url($url);
return $parts['scheme'] . '://' . $parts['host'] . $parts['path'];
}
foreach ($urls as $url) {
echo clean_url($url) . "\n";
}
Example:
例:
·> php url.php
http://example.com/foo
http://example.com/foo/bar/
http://example.com/foo/bar/baz
https://foo.example.com/foo/bar/baz
#2
64
You must use strrpos function not strpos ;-)
你必须使用strrpos函数而不是strpos ;-)
substr($url, 0, strrpos( $url, '/'));
#3
0
$cutoff = explode("char", $string);
echo $cutoff[0]; // 2 for what you want and 3 for the index
also
也
echo str_replace("http://", "", $str);
echo str_replace(“http://”,“”,$ str);
#4
0
Actually, a simpler solution to what you want to achieve is to play with a few string manipulation functions of PHP.
实际上,一个更简单的解决方案是你想要实现的是使用PHP的一些字符串操作函数。
First of all, you need to find the position of the last occurrence of the '/'. You can do this by using the strrpos() function (be careful, it is with 2 r);
首先,您需要找到最后一次出现'/'的位置。你可以使用strrpos()函数来做到这一点(小心,它是2 r);
Then, if you provide this position as a negative value, as a second parameter to the substr() function, it will start the search of the substring from the end.
然后,如果您将此位置作为负值提供,作为substr()函数的第二个参数,它将从末尾开始搜索子字符串。
The second problem is that you want as result the part of the strings that is at the LEFT of the last '/'. To achieve this, you will have to provide substr() with a negative value to the third parameter which will indicate how many characters you want to remove.
第二个问题是你想要的结果是最后一个'/'左边的字符串部分。要实现此目的,您必须为第三个参数提供带负值的substr(),这将指示要删除的字符数。
To be sure of how many parameters you need to remove, you will have to extract the string part at the RIGHT of the '/' first, and then count its length.
要确保需要删除多少个参数,首先必须在'/'的右边提取字符串部分,然后计算其长度。
//so given this url:
$current_url = 'http://example.com/firstslug/84'
//count how long is the part to be removed
$slug_tbr = substr($current_url, strrpos($current_url, '/')); // '/84'
$slug_length = strlen(slug_tbr); // (3)
/*get the final result by giving a negative value
to both second and third parameters of substr() */
$back_url = substr($current_url, -strrpos($current_url, '/'), -$slug_length);
//result will be http://example.com/firstslug/