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!?
I have this so far:
$url = substr($url, 0, strpos( $url, '/'));
But does not work, strips off everything after first '/'.
You should use the tool that is designed for this type of job, parse_url
url.php
Example:
Actually, a simpler solution to what you want to achieve is to play with a few string manipulation functions of 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);
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.
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.
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.
also
You must use strrpos function not strpos ;-)