How do I get to strip a url and put it back in the same position?
Use regular expressions.. you can easily detect the URL using regexs.
If you're wanting to put the same URL back into its initial position, why do you want to remove it in the first place/ what exactly are you trying to do. A little more context would help us help you.
There are not many ways to regexp an url that is compliant with rfc3986
As a C string, the regex will be:
"^(([^:/?#]+):)?(//([^/?#]*)|///)?([^?#]*)(\\?[^#]*)?(#.*)?"
enum {
URL = 0,
SCHEME_CLN = 1,
SCHEME = 2,
DSLASH_AUTH = 3,
AUTHORITY = 4,
PATH = 5,
QUERY = 6,
FRAGMENT = 7
};
Where the enum denotes what capture indexes correspond to which url parts.
you're probably looking for preg_replace_callback
.
it simply matches a regular expression pattern, gives the result to a function you provide, and replaces the original match with its return value.
Jeff Atwood had an informative post regarding the issues you might face with this:
http://www.codinghorror.com/blog/archives/001181.html
Regardless of the programming language and libraries you use, you will have problems if, say, with some cases --http://www.codinghorror.com/blog/archives/001181.html-- with a straightforward regex substitution.
I don't use PHP very often, but the issue boils down to trying to figure out the pattern to use in the preg_replace call.