HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Remove parameters from string containing URL

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
containingremovefromparametersstringurl

Problem

Is this the best way to remove URL parameters from a string containing an URL?

$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:

$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&parameter2=ho";
$url = strtok($url, "?");

Context

StackExchange Code Review Q#77024, answer score: 7

Revisions (0)

No revisions yet.