patternphpMinor
Remove parameters from string containing URL
Viewed 0 times
containingremovefromparametersstringurl
Problem
Is this the best way to remove URL parameters from a string containing an URL?
Is there a better one-line solution? It seems so bad to run the same function twice.
$url = "http://www.test.com/test.html?parameter=hey¶meter2=ho";
if (strstr($url, "?")) {
$url = strstr($url, "?", true);
}Is there a better one-line solution? It seems so bad to run the same function twice.
Solution
The simplest way, a true one-liner, would be to use strtok, an ancient but for some reason obscure function that does exactly what you need:
it will give you the part before the token if it is found, or the whole string otherwise
$url = "http://www.test.com/test.html?parameter=hey¶meter2=ho";
$url = strtok($url, "?");it will give you the part before the token if it is found, or the whole string otherwise
Code Snippets
$url = "http://www.test.com/test.html?parameter=hey¶meter2=ho";
$url = strtok($url, "?");Context
StackExchange Code Review Q#77024, answer score: 7
Revisions (0)
No revisions yet.