Jump to content

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!

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?

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.