1
\$\begingroup\$

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

$url = "http://www.test.com/test.html?parameter=hey&parameter2=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.

\$\endgroup\$
4
\$\begingroup\$

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

it will give you the part before the token if it is found, or the whole string otherwise

\$\endgroup\$
0
\$\begingroup\$

There are built-in functions for this purpose, in PHP.


First you split the different part of the url : $parsedUrl = parse_url($url);

Here is the part you're looking for : $queryPart = $parsedUrl["query"];

You can split this query in parts with parse_str($queryPart, $_QUERY);

And Voilà ! var_dump($_QUERY);

You now can remove what you want from the $_QUERY Array : unset($_QUERY['parameter']);

And construct the Query String back : $queryPart = http_build_query($_QUERY);

Put the query back in the $parsed_url : $parsedUrl["query"] = $_QUERY;

And finally reconstruct the url : http_build_url('', $parsedUrl["query"] );


Here you go : http://php.net/manual/en/function.parse-url.php

\$\endgroup\$
  • \$\begingroup\$ You seems to be missed what is asked here \$\endgroup\$ – Your Common Sense Apr 9 '18 at 7:15
0
\$\begingroup\$

You can use it to get the url.

$parsed = parse_url($url);
$finalUrl= $parsed['scheme']. '://'. $parsed['host']. $parsed['path'];

The main question was Remove parameters from string containing URL

So here on first line,

the url is parsed with parse_url() method it will return and array with four element

first index will be scheme

second one will be host

third one will be path

fourth one will be query

now on second line,

final url is build without the query parameter.

For safe side coading you can add condition to check valid url at first.

if (filter_var($url, FILTER_VALIDATE_URL)) {
    //-- valid url
}

so final code will be.

if (filter_var($url, FILTER_VALIDATE_URL)) {
    //-- valid url
    $parsed = parse_url($url);
    $finalUrl= $parsed['scheme']. '://'. $parsed['host']. $parsed['path'];
}

Thanks all

\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to CR! This answer triggered a low-quality flag; can you edit and expand the idea a bit? \$\endgroup\$ – Mathieu Guindon Apr 8 '18 at 16:52
  • \$\begingroup\$ @Mathieu, I have explain the answer in details, can I get positive flag now, \$\endgroup\$ – Shuvankar Paul Apr 9 '18 at 6:51
  • \$\begingroup\$ your answer needs a review as well. what's the use of unset() here? \$\endgroup\$ – Your Common Sense Apr 9 '18 at 7:11
  • \$\begingroup\$ @YourCommonSense , i am already describe that, the unset() function will not used in this solution. It will be used as concept if you need to use http_build_url() or http_build_str() function. and final code also place below. Thanks. if i am wrong please correct me. \$\endgroup\$ – Shuvankar Paul Apr 9 '18 at 8:55
  • \$\begingroup\$ This site is about better code and your own code is inconsistent, as unset function's result is not used anywhere. As of http_build_url(), it is not a core function and not available by default, which makes it's use questionable. Remove unset and http_build_url(). I would also suggest for you to write questions instead of answers for a time being. \$\endgroup\$ – Your Common Sense Apr 9 '18 at 9:11
-1
\$\begingroup\$
$url = 'http://www.test.com/test.html?parameter=hey&parameter2=ho';
if ( $temp = strstr($url, '?', true) ) {
   $url = $temp;
}

If you just want to get rid of the second call, you can save the value in a variable. If it exists, do the assignment. If not, don't.

In PHP, if you don't need to do string interpolation (variables embedded in strings), you can just use single quotes. This is slightly faster and can help avoid bugs where you accidentally interpret something as a variable that was meant to be a literal $.

Note: there may be better overall solutions, but with so little context, it is hard to see them. For example, you don't say how you get this URL. Or what you're going to do with it. There might be better changes in one of those places.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.