php - Get second number from (1_2_3_4) -


How to obtain a longer string than the second string:

  1200_500_test_5. 3_test2  

i.e.: I want to get only the second section in this word or number, in which there is _ between each of them.

You do not need to use regular expressions just use:

  $ input = '1200_500_test_5.3_test2'; $ Output = explosion ('_', $ input, 3); $ Output echo [1]; // 500  

but if you want to use a regular expression, use it:

  $ input = ' 1200_500_test_5.3_test2 '; Preg_match ('/ (? & Lt; = _) [^ _] + /', $ input, $ output); $ Echo output [0]; // 500  

or this:

  $ input = '1200_500_test_5.3_test2'; Preg_match ('/ (?: (?: [^ _] +) _) ([^ _] +) /', $ Input, $ output); $ Output echo [1]; // 3rd  

and group n to get the third group ( 2 with n-1 Em>:

  $ input = '1200_500_test_5.3_test2'; preg_match ('/ (?: (?: [^ _] +) _) {2} ([^ _] +) / ', $ Input, $ output); $ output echo [1]; // test  

Comments