从字符串中删除文本

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

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

(?ver=1.0)

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);
$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

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

$str = 'http://site.com/js/script.js?ver=1.0';
$str = substr($str, 0, strpos($str, '?'));

To remove the question mark and everything after it:

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

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