Reputation: 23
http://site.com/js/script.js?ver=1.0
How can I remove the query arguments from a string like this?
(?ver=1.0
)
Upvotes: 2
Views: 1232
Reputation: 675
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);
Upvotes: 1
Reputation: 219920
To remove the question mark and everything after it:
$string = 'http://site.com/js/script.js?ver=1.0';
$string = array_shift( explode('?', $string) );
Upvotes: 1
Reputation: 141829
Remove everything after (and including) the first ?
character:
$str = 'http://site.com/js/script.js?ver=1.0';
$str = substr($str, 0, strpos($str, '?'));
Upvotes: 4
Reputation: 4522
$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
Upvotes: 2