SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular expressions not working with new lines.

    I made a simple PHP script for testing. The script is as follows:
    PHP Code:
    <?PHP
    $content 
    "[loop:sections]test test[endloop:sections]";
    $content preg_replace('#\[loop:(\w+)\](.*?)\[endloop:\1\]#i''"\1, \2"'$content);
    echo 
    $content;
    ?>
    This script outputs (as it should) this:
    "sections, test test"
    Now if I have modify the input so the script looks like this:
    PHP Code:
    <?PHP
    $content 
    "[loop:sections]test \n test[endloop:sections]";
    $content preg_replace('#\[loop:(\w+)\](.*?)\[endloop:\1\]#i''"\1, \2"'$content);
    echo 
    $content;
    ?>
    the script now outputs this:
    [loop:sections]test test[endloop:sections]
    . Notice the newline...

    How would I make it so that what's in between [loop:whatever] and [endloop:whatever], be found, even if there is newlines in there?

    Thanks in advance

  2. #2
    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)
    when you're using the dot metacharacter (.) in your pattern, that means "match any character," as you probably know. well, actually, by default, it matches any character except newlines. so, to make . match newlines, you need to add the s modifier to your pattern. e.g. change the end of your pattern from

    #i

    to

    #is

    then it should work.
    - 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

  3. #3
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    alright, thanks!

    I seen the s modifier in the past but never understood what it actually meant. Thanks for the help!


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
  •