Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #15904
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: extract values from string |
| Date | 2015-11-15 09:47 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <n2a5pt$fuh$1@dont-email.me> (permalink) |
| References | (4 earlier) <n25rov$s66$1@speranza.aioe.org> <n261id$epq$1@dont-email.me> <n27316$vt0$2@speranza.aioe.org> <n27ehp$fo1$1@dont-email.me> <87io53uc44.fsf@ahungry.com> |
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
==================
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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