Results 1 to 12 of 12

Thread: [RESOLVED] Retrieving part of a string

  1. #1

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Resolved [RESOLVED] Retrieving part of a string

    Hi,

    Basically I want to extract "foo" from the string "(foo)bar", where the only constants are the parentheses.
    The method I'm using now:
    PHP Code:
    $tmp explode('('$input);
    $tmp2 explode(')'$tmp[1]);
    $output $tmp2[0]; 
    In my opinion very inefficient, but I can't think of anything else at the moment.

    Any hints?

    Thanks.
    Delete it. They just clutter threads anyway.

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Retrieving part of a string

    Welcome to the wonderful world of regular expressions.
    Code:
    $input="(foo)bar";
    preg_match("/[^(]*\(([^)]*)\).*/",$input,$matches);
    print_r($matches); //"foo" is in $matches[1]
    Regex is a big topic that I'd recommend Googling for an introduction and some examples to learn from. Its purpose is string processing based on flexible patterns.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Retrieving part of a string

    try:
    PHP Code:
    if (preg_match('/^\(([^\)])*\)/'$input$matches))
      
    $output $matches[1]; 
    (untested...)
    Last edited by penagate; Feb 25th, 2010 at 07:23 PM.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Retrieving part of a string

    Doh. Too slow.

  5. #5

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Retrieving part of a string

    Yeah, I figured it would be regex
    But my knowledge of it is really fragmented, still need to take some time to study that.

    If it isn't too much trouble, would you mind explaining the expression?
    What I seem to understand so far is that with [^(]* it matches anything that isn't brackets, right?

    Thanks anyway
    Delete it. They just clutter threads anyway.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Retrieving part of a string

    Square brackets make up a character class:

    [a] will match one instance of the character 'a'
    [ab] will match either 'a' or 'b'
    [a]* will match 'a' any number of times, including zero
    [ab]* will match '', 'a', 'b', 'ab', 'ba', 'aba', and so on


    The ^ symbol is used for negation:

    [^a] will match any character except for 'a'
    [^a]* will match any string which doesn't contain 'a'.

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Retrieving part of a string

    yes. the expression ([^)])* basically says to match zero or more characters (*) that are not (^) a closing bracket (")"). the brackets surrounding this expression mean that you want to store whatever is matched there. the end expression, .* says to match any character zero or more times (which would match bar in your example), but does not store it.

    oops! I guess penagate got here first ;)

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Retrieving part of a string

    Ha!



  9. #9
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Retrieving part of a string

    I could see you were both on the thread when I was going to reply... Figured I'd just sit back and watch. :]

  10. #10

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Retrieving part of a string

    I see. And what's the \( and \) for?

    Thanks for all the replies
    Delete it. They just clutter threads anyway.

  11. #11
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Retrieving part of a string

    backslashes in PHP are pretty much always for escaping characters. parenthesis in regular expressions are special characters, just like square brackets, the asterisk, period, and addition signs are. these must be escaped if you want to match for them in a regular expression. you're escaping them in this case because you want to match a string enclosed within them, hence \(([^)])*\).

  12. #12

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: [RESOLVED] Retrieving part of a string

    Thanks, that's clear now
    Delete it. They just clutter threads anyway.

Posting Permissions

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



Click Here to Expand Forum to Full Width