1

I have a string that looks like this, like a URL that has parameters.

folder/tested/file.js?p1=v1&p2=v2

How can I manipulate this string so as to remove all params, so that it ends up looking like this

folder/tested/file.js

5 Answers 5

5

Check out parse_url() - http://php.net/function.parse-url

$path = parse_url($url, PHP_URL_PATH);

Sign up to request clarification or add additional context in comments.

Comments

3
$array = explode("?", "folder/tested/file.js?p1=v1&p2=v2");
$array[0];

Comments

1

There's no need for the explode workaround in this case:

$path = strtok($url, "?");

Comments

0

Here is another method that is somewhat 'dirty':

$tmp = 'folder/tested/file.js?p1=v1&p2=v2';
$pos = strpos($tmp, '?');
$url = substr($tmp, 0, $pos);

Comments

-1

Try splitting by a '/' then by a '?' into two parts, and just taking what you need from both operations:

http://php.net/function.explode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.