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


Groups > comp.lang.php > #15893

Re: extract values from string

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: extract values from string
Date 2015-11-13 20:10 -0500
Organization A noiseless patient Spider
Message-ID <n261id$epq$1@dont-email.me> (permalink)
References <n24v7u$rol$1@speranza.aioe.org> <n2540n$kfj$1@dont-email.me> <n25dlg$tg0$1@speranza.aioe.org> <n25ig7$iir$1@dont-email.me> <n25rov$s66$1@speranza.aioe.org>

Show all headers | View raw


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
==================

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web