mstdmstdd Posted June 17, 2017 Share Posted June 17, 2017 Hello,I need to get all numbers from text with different format, like 23. or .76I did : $pattern= '~([\d]+\.[\d]+)'. '|'. '(\.[\d]+)'. '|'. '([\d]+)'. '|'. '([\d]+\.)~ium'; $str= 'Lorem 123 ipsum dolor 45.67 sit amet, consectetur 23.adipiscing elit, 0.45 sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea .76 commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'; But output was a bit different I expected : $matsches::Array ( [0] => Array ( [0] => 123 [1] => 45.67 [2] => 23 [3] => 0.45 [4] => .76 ) [1] => Array ( [0] => [1] => 45.67 [2] => [3] => 0.45 [4] => ) [2] => Array ( [0] => [1] => [2] => [3] => [4] => .76 ) [3] => Array ( [0] => 123 [1] => [2] => 23 [3] => [4] => ) [4] => Array ( [0] => [1] => [2] => [3] => [4] => ) ) Though [0] has valid results...Which is the right way?Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/304158-get-all-numbers-from-text-with-different-format/ Share on other sites More sharing options...
mac_gyver Posted June 17, 2017 Share Posted June 17, 2017 from the documentation - PREG_PATTERN_ORDEROrders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on. the zero'th element contains all the matches. the 1st element contains the matches from the first sub-pattern. the 2nd element contains matches from the second sub-pattern.... But output was a bit different I expected : what result were you expecting? 1 Quote Link to comment https://forums.phpfreaks.com/topic/304158-get-all-numbers-from-text-with-different-format/#findComment-1547476 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.