2

http://site.com/js/script.js?ver=1.0

How can I remove the query arguments from a string like this?

(?ver=1.0)

1
  • 1
    Perhaps $_SERVER['SCRIPT_FILENAME'] is an easier way to get to your string. Commented Sep 5, 2011 at 22:47

4 Answers 4

4

Remove everything after (and including) the first ? character:

$str = 'http://site.com/js/script.js?ver=1.0';
$str = substr($str, 0, strpos($str, '?'));
Sign up to request clarification or add additional context in comments.

Comments

2
$string = str_replace('?ver=1.0', '', $string);

Or if this example only represents a laundry list of additional query values, you could explode on the question mark and your first resulting array key will be the desired string.

$array = explode('?', $string);
echo $array[0]; // http://site.com/js/script.js

Comments

1

If you need to do this for different, longer, or possibly unknown query strings, take your pick from the following methods:

$substr = substr($string, 0, strpos($string, '?'));

$regex = preg_replace('/\?(.*)/', '', $string);

$array = explode('?', $string);
$str = current($array);

Comments

1

To remove the question mark and everything after it:

$string = 'http://site.com/js/script.js?ver=1.0';

$string = array_shift( explode('?', $string) );

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.