Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #15888 > unrolled thread

extract values from string

Started byalbert <alstopspam@stopaspam.com>
First post2015-11-13 16:22 +0100
Last post2015-11-17 13:02 +0000
Articles 11 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  extract values from string albert <alstopspam@stopaspam.com> - 2015-11-13 16:22 +0100
    Re: extract values from string Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-13 11:46 -0500
      Re: extract values from string albert <alstopspam@stopaspam.com> - 2015-11-13 20:28 +0100
        Re: extract values from string Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-13 15:53 -0500
          Re: extract values from string albert <alstopspam@stopaspam.com> - 2015-11-14 00:29 +0100
            Re: extract values from string Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-13 20:10 -0500
              Re: extract values from string albert <alstopspam@stopaspam.com> - 2015-11-14 11:39 +0100
                Re: extract values from string Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-14 08:58 -0500
                  Re: extract values from string Matthew Carter <m@ahungry.com> - 2015-11-14 23:20 -0500
                    Re: extract values from string Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-15 09:47 -0500
                    Re: extract values from string Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-17 13:02 +0000

#15888 — extract values from string

Fromalbert <alstopspam@stopaspam.com>
Date2015-11-13 16:22 +0100
Subjectextract values from string
Message-ID<n24v7u$rol$1@speranza.aioe.org>
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

[toc] | [next] | [standalone]


#15889

FromJerry Stuckle <jstucklex@attglobal.net>
Date2015-11-13 11:46 -0500
Message-ID<n2540n$kfj$1@dont-email.me>
In reply to#15888
On 11/13/2015 10:22 AM, albert wrote:
> 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

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
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#15890

Fromalbert <alstopspam@stopaspam.com>
Date2015-11-13 20:28 +0100
Message-ID<n25dlg$tg0$1@speranza.aioe.org>
In reply to#15889
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


[toc] | [prev] | [next] | [standalone]


#15891

FromJerry Stuckle <jstucklex@attglobal.net>
Date2015-11-13 15:53 -0500
Message-ID<n25ig7$iir$1@dont-email.me>
In reply to#15890
On 11/13/2015 2:28 PM, albert wrote:
> 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
> 
> 
> 

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?

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

[toc] | [prev] | [next] | [standalone]


#15892

Fromalbert <alstopspam@stopaspam.com>
Date2015-11-14 00:29 +0100
Message-ID<n25rov$s66$1@speranza.aioe.org>
In reply to#15891
> 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).
oh yes

> Does this get you on the right track?

ok
very very thanks

[toc] | [prev] | [next] | [standalone]


#15893

FromJerry Stuckle <jstucklex@attglobal.net>
Date2015-11-13 20:10 -0500
Message-ID<n261id$epq$1@dont-email.me>
In reply to#15892
On 11/13/2015 6:29 PM, albert wrote:
>> 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).
> oh yes
> 
>> Does this get you on the right track?
> 
> ok
> very very thanks
> 
> 

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";

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

[toc] | [prev] | [next] | [standalone]


#15894

Fromalbert <alstopspam@stopaspam.com>
Date2015-11-14 11:39 +0100
Message-ID<n27316$vt0$2@speranza.aioe.org>
In reply to#15893
> 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?

[toc] | [prev] | [next] | [standalone]


#15899

FromJerry Stuckle <jstucklex@attglobal.net>
Date2015-11-14 08:58 -0500
Message-ID<n27ehp$fo1$1@dont-email.me>
In reply to#15894
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.


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

[toc] | [prev] | [next] | [standalone]


#15903

FromMatthew Carter <m@ahungry.com>
Date2015-11-14 23:20 -0500
Message-ID<87io53uc44.fsf@ahungry.com>
In reply to#15899
Jerry Stuckle <jstucklex@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

[toc] | [prev] | [next] | [standalone]


#15904

FromJerry Stuckle <jstucklex@attglobal.net>
Date2015-11-15 09:47 -0500
Message-ID<n2a5pt$fuh$1@dont-email.me>
In reply to#15903
On 11/14/2015 11:20 PM, Matthew Carter wrote:
> Jerry Stuckle <jstucklex@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.
> 

IFF you understand regexes.  How many new PHP programmers understand them?


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

[toc] | [prev] | [next] | [standalone]


#15906

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-11-17 13:02 +0000
Message-ID<n2f8h9$sh9$2@dont-email.me>
In reply to#15903
On Sat, 14 Nov 2015 23:20:59 -0500, Matthew Carter wrote:

> Jerry Stuckle <jstucklex@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, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web