16

Can anyone tell me how to remove characters after ? in php. I have a string test?q=new and I need to remove the characters from the ? to the end of the string.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Anish
  • 4,262
  • 6
  • 36
  • 58

7 Answers7

38

Shortest one:

echo strtok('test?=new', '?');

If you want to keep the question mark, the solution is almost the same:

echo strtok('test?=new', '?').'?';
Rok Kralj
  • 46,826
  • 10
  • 71
  • 80
5

You could always try using preg_replace() as well:

$string = 'test?q=new';
$result = preg_replace("/\?.+/", "", $string);

If, for some reason, you are wanting to keep the ? in the result... you could also do this:

$string = 'test?q=new';
$result = preg_replace("/\?.+/", "?", $string);

(or, you could use a positive look-behind assertion, as @BlueJ774 suggested,) like this:

$result = preg_replace("/(?<=\?).+/", "", $string);

But ideally, and for future reference, if you are working with a query string, you probably will want to use parse_str at some point, like this:

$string = 'test?q=new';
parse_str($string, $output);

Because that will give you an array ($output, in this case,) with which to work with all of the parts of the query string, like this:

Array
(
    [test?q] => new
)

But normally... you would probably just want to be working with the query string by this point... so the output would be more like this:

Array
(
    [q] => new
)
summea
  • 7,390
  • 4
  • 32
  • 48
  • 1
    The second `preg_replace` (where the `?` is kept) could also be represented as `$result = preg_replace("/(?<=\?).+/", "", $string);` I think the intent is clearer that way. –  Feb 16 '12 at 18:23
  • Good point. In a regular script, that would make more sense; it's just for the sake of this example, I kept both of the expressions the same for simplicity. But I would imagine the `?` would normally not be saved... – summea Feb 16 '12 at 18:27
2

Why not:

$pos = strpos($str, '?'); // ? position
$str = substr($str, 0, $pos);
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

You can do this with a well-written regex, but the much simpler and quicker way to do it is to explode the string on the "?" character, and use the first element in the resulting array.

$str = "test?=new";
$str2 = explode("?", $str);
$use_this = $str2[0];

$use_this[0] will be "test". If you want to add the "?" back, just concatenate:

$use_this = $use_this."?";
swayne
  • 84
  • 3
1

Here is one-liner:

$s = strpos($s, '?') !== FALSE ? strtok($s, '?') : $s;

You can test it by the following command-line:

php -r '$s = "123?456"; $s = strpos($s, "?") !== FALSE ? strtok($s, "?") : $s; echo $s;'
kenorb
  • 155,785
  • 88
  • 678
  • 743
0

Use the strstr function.

<?php
$myString = "test?=new";
$result = strstr($myString, '=', true);

echo $result ;

The third parameter true tells the function to return everything before the first occurrence of the second parameter.

Faisal
  • 4,591
  • 3
  • 40
  • 49
0

substr and strpos

The simplest way to do this is with substr() DOCs and strpos() DOCs.

$string = 'test?=new';
$cut_position = strpos($string, '?') + 1; // remove the +1 if you don't want the ? included
$string = substr($string, 0, $cut_position);

As you can see substr() extracts a sub-string from a string by index and strpos() returns the index of the first instance of the character it is searching for (in this case ?).

Treffynnon
  • 21,365
  • 6
  • 65
  • 98