Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #15903
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Matthew Carter <m@ahungry.com> |
| Newsgroups | comp.lang.php |
| Subject | Re: extract values from string |
| Date | Sat, 14 Nov 2015 23:20:59 -0500 |
| Organization | Ahungry (http://ahungry.com) |
| Lines | 55 |
| Message-ID | <87io53uc44.fsf@ahungry.com> (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> <n261id$epq$1@dont-email.me> <n27316$vt0$2@speranza.aioe.org> <n27ehp$fo1$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| Injection-Info | mx02.eternal-september.org; posting-host="7c986cd4736462de309a749b207746fe"; logging-data="12886"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/0FgcisAQTi88fKkROlIGU" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) |
| Cancel-Lock | sha1:ZDQgAWjBAqNeE9gXRTfqHCkocb4= sha1:iH1pyE89RD85R/ptSlvWoufW2y4= |
| Xref | csiph.com comp.lang.php:15903 |
Show key headers only | View raw
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
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