preg_replace and new line?!

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

  1. #1
    I see if if the first search braket is found on a line, and the last search breaket on another line... then the preg_replace won't work ....
    
    
    $string="
    FILE 
    THIS WILL NOT WORK
    SECTION
    ";
    
    $search='#(FILE)(.*)(SECTION)#e';
    $output = preg_replace($search,"('')",$string);
    
    
    
    $string=" FILE  BUT THIS WILL WORK ...  SECTION ";
    
    $search='#(FILE)(.*)(SECTION)#e';
    $output = preg_replace($search,"('')",$string);
    
    
    Code (markup):

     
    redhits, Oct 26, 2009 IP
  2. heavydev

    heavydev Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    heavydev, Oct 26, 2009 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    You actually need the "s" modifier to make the dot (.) match new lines.
     
    nico_swd, Oct 27, 2009 IP
  4. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #4
    Can somebody give me a short example?

    how this : $search='#(FILE)(.*)(SECTION)#e';
    should be changed?
     
    redhits, Oct 27, 2009 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    
    $search='#FILE(.*?)SECTION#es';
    
    PHP:
    ... are you sure you actually need the "e" (eval) modifier? Seeing your above code, I think you could get rid of it.
     
    nico_swd, Oct 27, 2009 IP
  6. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    szalinski, Oct 27, 2009 IP
  7. eamong

    eamong Well-Known Member

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    108
    #7
    (.*) will not check \r and or \n
     
    eamong, Oct 28, 2009 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It will with the s modifier at the end
     
    JAY6390, Oct 28, 2009 IP