Jump to content

get all numbers from text with different format


mstdmstdd

Recommended Posts

  Hello,
I need to get all numbers from text with different format, like 23. or .76
I 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!

Link to comment
Share on other sites

from the documentation - 
 

PREG_PATTERN_ORDER
Orders 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?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.