How to find text inside { } in a string

Discussion in 'PHP' started by Jona, Oct 26, 2009.

  1. #1
    Below is a sample text string ...

    $test = "This is {just a small} sample text. This can actually be {very long} but in {this case it is short}.";

    I need to get the text inside the { } into an array:
    The string length is not fixed and it can be hundreds of { } in another sample...


    Expected result:

    $arr[0] = "just a small";
    $arr[1] = "very long";
    $arr[2] = "this case it is short";

    Any idea how to do this in a fast and effectiv way ?
     
    Last edited: Oct 26, 2009
    Jona, Oct 26, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    preg_match_all('%\{([^}]+)\}%',$input_text, $matches);
    $arr = $matches[1];
    PHP:
     
    JAY6390, Oct 26, 2009 IP
  3. Jona

    Jona Well-Known Member

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Thank's! Work's like a charm !!!
     
    Jona, Oct 26, 2009 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    My pleasure :)
     
    JAY6390, Oct 26, 2009 IP