You can always try using preg_replace() :
$string = 'test?q=new'; $result = preg_replace("/\?.+/", "", $string);
If for some reason you want to save ? as a result ... you can also do this:
$string = 'test?q=new'; $result = preg_replace("/\?.+/", "?", $string);
(or you could use the positive look-behind statement, as @ BlueJ774 suggested):
$result = preg_replace("/(?<=\?).+/", "", $string);
But ideally, and for future reference, if you are working with a query string, you probably want to use parse_str at some point, for example:
$string = 'test?q=new'; parse_str($string, $output);
Because it will give you an array ( $output , in this case) that you need to work with all parts of the query string, for example:
Array ( [test?q] => new )
But usually ... you probably just want to work with the query string at this point ... so the result would be something like this:
Array ( [q] => new )
summea Feb 16 '12 at 17:26 2012-02-16 17:26
source share