Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

extract values from string

16 views
Skip to first unread message

albert

unread,
Nov 13, 2015, 10:22:46 AM11/13/15
to
if I have this
xx/yy/178 1771 80 279
or
xx/yy/122
or
xx/yy/

how can I extract numbers separated by a space and insert in one array?
in the case I know the initial two values
xx/yy/


and when I not know the initial two values xx/yy/,example I can also to
have p/c/ or others value


........................
other sinilar
extrct from
pppp/aaaa/$ this pppp/aaaa
think is necessary to extract everything except the last 2 terms;
is so and how

Jerry Stuckle

unread,
Nov 13, 2015, 11:46:36 AM11/13/15
to
Albert,

I'm a little confused as to what you're looking for.

When you say "extract numbers...", do you mean you want to use the
numbers and throw away the rest (the common meaning of extract), or do
you want to throw away the numbers and save the rest?

For instance, if you have:

xx/yy/178 1771 80 279

The common meaning of "extract numbers..." would give you

178 1771 80 279

possibly as four elements of an array (I'm not sure if you want four
elements or all four numbers in one element).

But from your comments, I think you want

xx/yy/

The question here would be - is it always two substrings separated by a
'/' and ending with a '/'?

If the latter, I think the easiest way would be to find the location of
the second '/' with two calls to strpos, i.e.

function findval($str) {
$pos = strpos($str, '/'); // Find first '/'
if ($pos === false)
return false; // Or you could return $str
$pos = strpos($str, '/', $pos+1);
if ($pos === false)
return false; // Or you could return $str
return substr($str, 0, $pos-1);
}

If you're looking for something else, please clarify.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================

albert

unread,
Nov 13, 2015, 2:29:00 PM11/13/15
to
1st case
If have this:
xx/yy/178 1771 80 279

to have this
array[1]=178
array[2]=1771
array[3]=80
array[4]=279

I know this xx/yy/
but not know the numbers' quantity
I can also to have this
xx/yy/200 13
or this
xx/yy/122

2nd case
..../.../178 1771 80 279
not know the initial value, only know that the numbers start after
the last /



> The question here would be - is it always two substrings separated by a
> '/' and ending with a '/'?

yes



Jerry Stuckle

unread,
Nov 13, 2015, 3:53:48 PM11/13/15
to
OK, this isn't too hard. We take the code from my previous post and
modify it a bit to get the numbers into a string, then explode() the string:

function findvals($str) {
$pos = strpos($str, '/'); // Find first '/'
if ($pos === false)
return false; // Or you could return $str
$pos = strpos($str, '/', $pos+1);
if ($pos === false)
return false; // Or you could return $str
$vals = explode(" ", substr($str, $pos+1));
return $vals;
}

$data = findvals("xx/yy/178 1771 80 279");
if ($data)
print_r($data);
else
echo "Error!\n";

Output:

Array
(
[0] => 178
[1] => 1771
[2] => 80
[3] => 279
)

(Remember, PHP array indicies start with 0, not 1).

Does this get you on the right track?

albert

unread,
Nov 13, 2015, 6:29:48 PM11/13/15
to
oh yes

> Does this get you on the right track?

ok
very very thanks


Jerry Stuckle

unread,
Nov 13, 2015, 8:10:55 PM11/13/15
to
After further thinking about this, here's a shorter version which uses
explode() twice and might work for you, also:

function findvals($str) {
$nums = explode('/', $str);
if (count($nums) != 3)
return false;
$vals = explode(" ", $nums[2]);
return $vals;
}

$data = findvals("xx/yy/178 1771 80 279");
if ($data)
print_r($data);
else
echo "Error!\n";

albert

unread,
Nov 14, 2015, 5:39:49 AM11/14/15
to
> After further thinking about this, here's a shorter version which uses
> explode() twice and might work for you, also:
>
> function findvals($str) {
> $nums = explode('/', $str);
> if (count($nums) != 3)
> return false;
> $vals = explode(" ", $nums[2]);
> return $vals;
> }
>
> $data = findvals("xx/yy/178 1771 80 279");
> if ($data)
> print_r($data);
> else
> echo "Error!\n";
>


which is more fast?

Jerry Stuckle

unread,
Nov 14, 2015, 8:58:38 AM11/14/15
to
Speed is not as important as readability and maintainability. Write
your code so you and others can understand and maintain it. If you have
problems with speed in the future, then determine where the problems lie
and fix those.

Matthew Carter

unread,
Nov 14, 2015, 11:21:06 PM11/14/15
to
Jerry Stuckle <jstu...@attglobal.net> writes:

> On 11/14/2015 5:39 AM, albert wrote:
>>> After further thinking about this, here's a shorter version which uses
>>> explode() twice and might work for you, also:
>>>
>>> function findvals($str) {
>>> $nums = explode('/', $str);
>>> if (count($nums) != 3)
>>> return false;
>>> $vals = explode(" ", $nums[2]);
>>> return $vals;
>>> }
>>>
>>> $data = findvals("xx/yy/178 1771 80 279");
>>> if ($data)
>>> print_r($data);
>>> else
>>> echo "Error!\n";
>>>
>>
>>
>> which is more fast?
>
> Speed is not as important as readability and maintainability. Write
> your code so you and others can understand and maintain it. If you have
> problems with speed in the future, then determine where the problems lie
> and fix those.

This is more readable IMHO:

<?php

$tests[] = "xx/yy/178 1771 80 279";
$tests[] = "xx/yy/200 13";
$tests[] = "xx/yy/122";

$extractFn = function($in) { preg_match_all ('!(\d+)!', preg_replace ('!.*/!', '', $in), $m); return $m[1]; };

$results = array_map($extractFn, $tests);
var_dump ($results);

The extent of the Regexp is very simple (in the inner preg_replace, it
is getting rid of everything from the start of the string to the final
slash '/'.

In the preg_match_all, it is matching on all digits and storing in an
array.

The array_map is used over a foreach for clarity and as an (in general)
better practice, for variable scope encapsulation.

--
Matthew Carter (m...@ahungry.com)
http://ahungry.com

Jerry Stuckle

unread,
Nov 15, 2015, 9:47:46 AM11/15/15
to
IFF you understand regexes. How many new PHP programmers understand them?

Denis McMahon

unread,
Nov 17, 2015, 8:05:04 AM11/17/15
to
On Sat, 14 Nov 2015 23:20:59 -0500, Matthew Carter wrote:

> Jerry Stuckle <jstu...@attglobal.net> writes:

>> function findvals($str) {
>> $nums = explode('/', $str);
>> if (count($nums) != 3)
>> return false;
>> $vals = explode(" ", $nums[2]);
>> return $vals;
>> }

> This is more readable IMHO:

> $extractFn = function($in) { preg_match_all ('!(\d+)!', preg_replace
> ('!.*/!', '', $in), $m); return $m[1]; };

IMHO YHO is wrong.

--
Denis McMahon, denismf...@gmail.com
0 new messages