SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot sifuhall's Avatar
    Join Date
    Jun 2001
    Location
    out of the sidehatch
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    string parsing help

    I'm having trouble getting my head around this one. Any help would be greatly appreciated.

    I have a string that has certain areas delimited, for example:

    PHP Code:
    New entry %%test%%, and that is not all %%new test%%. 
    In this case the delimiter is %% (but I can make it anything if it makes it easier) and I would like to pull out test and new test into an array.

    Any suggestions?

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:

    PHP Code:
    <?php
    $data 
    'New entry %%test%%, and that is not all %%new test%%.';

    preg_match_all("/%%(.+?)%%/"$data$matches);

    print_r($matches); // this should contain test and new test
    ?>

  3. #3
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd use,
    PHP Code:
    $data 'New entry %%test%%, and that is not all %%new test%%.';
    preg_match_all("/%%([^%]+?)%%/i"$data$matches);
    foreach (
    $matches[1] as $a)
       echo 
    $a
    Paul
    [edit] Insert the i modifier to match both uper and lower case text.
    Last edited by Paul S; May 2, 2002 at 09:47.

  4. #4
    SitePoint Zealot sifuhall's Avatar
    Join Date
    Jun 2001
    Location
    out of the sidehatch
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    works great, thanks a million!

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Paul S
    [edit] Insert the i modifier to match both uper and lower case text.
    in this case there's no letters in the pattern, so the `i' modifier isn't needed and doesn't do anything.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  6. #6
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by DR_LaRRY_PEpPeR


    in this case there's no letters in the pattern, so the `i' modifier isn't needed and doesn't do anything.
    Uhh?? What do you mean by "there's no letters in the pattern"? isn't the pattern the entire string you are matching, ie, %%test%% or %%new test%%?

    Paul

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope, that's the string. this is the pattern

    /%%([^%]+?)%%/

    no letters there.

    now if you had

    /%%([a-z]+?)%%/

    then you would want to use the `i' modifier.

    BTW, this pattern

    /%%([^%]+?)%%/

    doesn't need the `?' after the quantifier (`+') because the quantifier can't be greedy, as its preceding pattern is `[^%]' (e.g. it can't "go past" the next `%'). you only need the ungreedy `?' with the `.' metacharacter (assuming you aren't using the `U' pattern-modifier).

    in conclusion, the pattern could be

    /%%(.+?)%%/

    or

    /%%([^%]+)%%/

    i prefer the former, since i think it looks cleaner.

  8. #8
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ohh!! I didn't know that, thanks
    (Where did you learn that?)

    Paul

  9. #9
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    learn what...? the regualr expression stuff i learned from Perl (Perl Black Book actually). then i just had to skim over the PHP manual page for preg syntax to take note of any differences from Perl. it's mostly the same, which i like. it even supports at least one thing that Perl doesn't! (variable length look-behind assertions ) this pattern wouldn't work in Perl

    /(?<=a|bc)foo/

    but does with PHP, yes! don't know how i would've done it in Perl.

    BTW, that pattern matches `foo' if it's preceded by `a' or `bc' (but `a' or `'bc' doesn't become part of the match, it's just tested). `a' and `bc' are different lengths, which isn't allowed in Perl.
    Last edited by DR_LaRRY_PEpPeR; May 2, 2002 at 15:13.


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
  •