Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #8750
| From | Christoph Becker <cmbecker69@gmx.de> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: split array and string from string (trust me it will make sense when you read) |
| Date | 2013-03-20 02:21 +0100 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <kib2tl$v84$1@speranza.aioe.org> (permalink) |
| References | <a9993724-7709-41e0-9412-02ed3056d12d@googlegroups.com> <ki9np5$2ll$3@dont-email.me> <3514efbb-74a4-41e7-8fac-2092a94cbc95@googlegroups.com> <kib0o8$sp9$1@dont-email.me> <79259447-b171-4dc0-8a8b-4a0ee86aeae0@googlegroups.com> |
CloudPress wrote:
> I guess the real question is is there anything to be gained by using
> regular expressions. besides cleaner code? Because if this will work
> without any noticeable performance difference compared to regex it might
> just be easier to stick to this.
IMO it's not so much about cleaner code, than about performance. Using
regexp's will shift all the hard work from PHP to C, which is much
faster. Consider the code you've posted: it calls strpos(), substr(),
str_replace() et al. in a loop multiple times; for sure not the fastest
way of doing things.
A quick example, what can be accomplished with a simple regexp:
>>> $str = 'Hello world, [key=value] [key2=value1] This is just a
test. [key=value2]';
>>> echo preg_replace('/\[key[0-9]*=value[0-9]*\]/', '', $str);
Hello world, This is just a test.
To fill the array of key/value pairs, you can start with:
>>> preg_match_all('/\[(key[0-9]*)=(value[0-9]*)\]/', $str, $m);
>>> var_dump($m)
--
Christoph M. Becker
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
split array and string from string (trust me it will make sense when you read) CloudPress <cloudpresshosting@gmail.com> - 2013-03-18 21:12 -0700
Re: split array and string from string (trust me it will make sense when you read) CloudPress <cloudpresshosting@gmail.com> - 2013-03-18 21:17 -0700
Re: split array and string from string (trust me it will make sense when you read) Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-19 09:06 -0400
Re: split array and string from string (trust me it will make sense when you read) CloudPress <cloudpresshosting@gmail.com> - 2013-03-19 17:34 -0700
Re: split array and string from string (trust me it will make sense when you read) Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-19 20:45 -0400
Re: split array and string from string (trust me it will make sense when you read) CloudPress <cloudpresshosting@gmail.com> - 2013-03-19 17:56 -0700
Re: split array and string from string (trust me it will make sense when you read) Christoph Becker <cmbecker69@gmx.de> - 2013-03-20 02:21 +0100
Re: split array and string from string (trust me it will make sense when you read) Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-19 21:43 -0400
csiph-web