SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Fastest / most efficient way to grab everything between { and } ?

    I'm trying to create a function that accepts 1 string for an argument and then takes everything between curly braces throughout the string. So for example, if this supposed function I speak of accepts the following:

    "BLAH BLAH {BLAH BLAH BLAH} BLAH BLAH {BLAH BLAH}!"

    It would then create another variable (array?) with the following in it:
    0=>'BLAH BLAH BLAH'
    1=>'BLAH BLAH'

    (I'm sure you get my point here.)

    Anyway, I'm wondering if using substr() is the best to use for this? Or should I use some pattern expression? I've read multiple places that using a pattern is least efficient and most time consuming, but I wanted to run this by you guys to see what you all thought.

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2010
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd go with a regexp here without any doubt.
    Optimizing something on this level probably won't affect performance of your application in any way.
    Yes, you can achieve the same end result with str_ functions, however nothing guarantees that the code done with str_ functions will be any more readable / maintainable than using a simple regexp.
    In terms of performance, you won't lose much processing power (we're talking about milliseconds here, not a single web user can claim they notice a difference between 0.001 and 0.002 seconds of execution time).

  3. #3
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let Google be your friend

    Here's some example code on how do do it.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Blue. View Post
    I'd go with a regexp here without any doubt.
    Optimizing something on this level probably won't affect performance of your application in any way.
    Me too, if only in terms of "developer efficiency"—it's a quick one-liner to get the array of strings with preg_match_all. While certainly possible (and not-so-difficult) with basic string functions, it's certainly not a single function call. Then again, if the OP has no idea what a quantifier or character class is then it may not be so "efficient".

    Quote Originally Posted by Blue. View Post
    In terms of performance, you won't lose much processing power (we're talking about milliseconds here, not a single web user can claim they notice a difference between 0.001 and 0.002 seconds of execution time).
    There may be sizable gains/losses in "performance", though to make any hints in one direction or the other would be very much premature since we have no idea of the intended use of any approaches that we offer here nor the myriad of variables which come together to affect said performance.


    A few questions for Wolf_22:

    • how familiar are you with regular expressions?
    • ditto for the various string functions?
    • what is prompting this desire to be "efficient" and "fast"?
    • is your BLAH example representative of the text that you will really be using?
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Addict Shaydez's Avatar
    Join Date
    Jul 2006
    Location
    Boca Raton, Florida
    Posts
    356
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    could use explode

  6. #6
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    While certainly possible (and not-so-difficult) with basic string functions, it's certainly not a single function call.
    The function itself would have multiple lines of code (but not many going by the example I googled quickly earlier).

    Once the function is set up to do what it needs to do then you could include it in either a php class file which handles "non standard" string functions or put it in a php library file after which it to would be a single function call whenever you want to use it.

    In reference to performance issues, I doubt very much the difference in execution time would be noticable unless the string was extremely long.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Your fine with preg_match_all()

    Don't make problems more complicated than they need to be unless no other alternative is present.

    simple, stupid – the next person looking at your code with thank you

    PHP Code:
    <?php
    $matches 
    = array();
    $str 'BLAH BLAH {my} BLAH BLAH {variables}!

    BLAH BLAH {are} BLAH BLAH {within}!

    BLAH BLAH {curly} BLAH BLAH {braces}!'
    ;

    preg_match_all('/\{(.*?)\}/xs',$str,$matches);

    echo 
    '<pre>',print_r($matches),'</pre>';
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •