Delete everything after character in PHP

Can anyone tell how to remove characters after? in php. Do I have one row test? = New I need to remove the characters as well as = from this line.

+12
php
Feb 16 '12 at 17:17
source share
8 answers

The shortest:

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

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

 echo strtok('test?=new', '?').'?'; 
+20
Feb 16 '12 at 17:21
source share

Does this solution use a simple regular expression to remove the character ? and all subsequent characters.

 $string = "test?p=new"; $new_string = preg_replace("/\?.+/", "", $string); 
+3
Feb 16 2018-12-12T00:
source share

Why not:

 $pos = strpos($str, '?'); // ? position $str = substr($str, 0, $pos); 
+2
Feb 16 '12 at 17:21
source share

You can do this with a well-written regular expression, but a much simpler and faster way to do this is to blow the string at "?" 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 "?" back, just connect:

 $use_this = $use_this."?"; 
+2
Feb 16 '12 at 17:23
source share

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 ) 
+1
Feb 16 '12 at 17:26
source share

Here is a single line:

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

You can test it on the following command line:

 php -r '$s = "123?456"; $s = strpos($s, "?") !== FALSE ? strtok($s, "?") : $s; echo $s;' 
+1
May 13 '14 at 3:50
source share

substr and strpos

The easiest way to do this is 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 substring from a string by index and strpos() returns the index of the first instance of the character it is looking for (in this case ? ).

0
Feb 16 '12 at 17:21
source share

Use the strstr function.

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

The third parameter true tells the function to return everything until the first appearance of the second parameter.

0
Jun 02 '17 at 15:39
source share



All Articles