SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Only get text within brackets?!?

    I'm trying to figure out how to extract only text within brackets []?

    This is what I have tried so far:
    PHP Code:
    $str "I only want this number, [256] to be in my string!";

    $out preg_match("/[(.*?)]/"$str);

    echo 
    'What comes out? '.$out
    I know now that preg_match is NOT the solution... But how then?

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    You're doing it wrong: preg_match

    Have a read of that page (the way you're using the function isn't correct for your needs) and also the linked pages outlining the PCRE regular expression syntax (the regex you've wrote doesn't do what you think it should be doing).
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $str "I only want this number, [256] to be in my string!"
    preg_match_all("/\[(.*?)\]/"$str$matches); 
    echo 
    $matches[1][0]; 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    PHP Code:
    $str "I only want this number, [256] to be in my string!"
    preg_match_all("/\[(.*?)\]/"$str$matches); 
    echo 
    $matches[1][0]; 
    Ah yes... Thanks alot ;-)

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of only quick fix, better to read the manual page once to find how the two functions preg_match and preg_match_all are differently work as salathe said.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


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
  •