FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » split array and string from string (trust me it will make sense when you read)
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: split array and string from string (trust me it will make sense when you read) [message #180814 is a reply to message #180813] Wed, 20 March 2013 01:21 Go to previous messageGo to previous message
Christoph Becker is currently offline  Christoph Becker
Messages: 91
Registered: June 2012
Karma:
Member
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Thorny string translation issue.
Next Topic: PDO based Abstraction Layer for MySQL with Caching
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 11:25:11 GMT 2024

Total time taken to generate the page: 0.04250 seconds