Php explode - need a second element

more interested ...

$_GET['unique'] = blahblahblah=this_is_what_im_interested_in 

I know that I can get the second element as follows:

 $words = explode('=', $_GET['unique']); echo $words[1]; 

Is there any way to get this on one line? - this might β€œhope” to allow me to add this to a function / object call:

 $common->resetPasswordReply(... in here I would put it....); 

like

 $common->resetPasswordReply(explode('=', $_GET['unique'])[1]); 

I'm just curious to know if this is possible.

+4
source share
3 answers

PHP supports indexing functions if they return arrays / objects; so the following will work:

 echo explode('=', $_GET['unique'])[1]; 

EDIT

This is called array dereferencing and is described in PHP docs:

According to PHP 5.4, you can mass dereference a result by calling a function or method. Before this was only possible using a temporary variable.

As with PHP 5.5, you can massage the dereferencing of an array literal.

+7
source

That should do it for you.

 substr($str, strpos($str, "=")+1); 
+1
source

Oddly enough, you can almost do this with list () , but you cannot use it in a function call. I only send it as you say 'more out of interest' : -

 $_GET['unique'] = "blahblahblah=this_is_what_im_interested_in"; list(, $second) = explode('=', $_GET['unique']); var_dump($second); 

Output: -

 string 'this_is_what_im_interested_in' (length=29) 

You can see good examples of how flexible list() is in the first set of examples on the page.

I think it's worth noting that although your example will work: -

 $common->resetPasswordReply(explode('=', $_GET['unique'])[1]); 

it really messes up your code, and it’s not obvious that you are going into this function. While something like the following is more readable: -

 list(, $replyText) = explode('=', $_GET['unique']); $common->resetPasswordReply($replyText)); 

Consider returning to your code after 6 months and trying to debug it. Make it as standalone documentation as possible. Also, do not forget that when you enter user input here, it will need to be sanitized at some point.

+1
source

Source: https://habr.com/ru/post/1485738/


All Articles