PHP explode gets specfic index in one line

I want to make

<?php $str = "I want to access 2nd or 3rd index in one line"; echo explode(" ",$str)[2]; ?> 

We can easily access the first index using

 stristr($str," ",true); //For php version >= 5.3 

or

 $foo = array_shift(explode(':', $foo)); 

or

 list($str) = explode(" ", $str); 

BUT

HOW TO ACCESS A SPECIFIC INDEX [1], [2] OR [3] IN ONE LINE

+4
source share
3 answers

5.4 +

 <?php echo explode(" ","I want to access 2nd or 3rd index in one line")[2]; ?> 
+2
source

for the second

 strtok($string, " "); echo strtok(" "); 

or third

 strtok($string, " "); strtok(" "); echo strtok(" "); 
0
source

Try this to get a specific index value.

 <?php $str = "Iam want to access 2nd or 3rd index in one line"; $val = explode(" ",$str); echo $val[3]; ?> 
-1
source

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


All Articles