preg_match help

Discussion in 'PHP' started by samie, Jan 16, 2013.

  1. #1
    Hey Guys,

    I was just wondering if anybody could help me with a little project I am working on.

    I have a txt file that I am trying to run a preg_match on to find patterns that start with with a certain phrase, and then end with a certain phrase and then have both phrases deleted as well as everything in between.

    So just as an example, say this following is the text in my file.

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. <onions> Donec ac nunc turpis, ut accumsan turpis. In </oranges> vitae tellus nulla. Duis iaculis enim leo. Nulla sagittis vestibulum dictum. Quisque eget facilisis libero. Nam tempor pretium malesuada. Suspendisse eros metus, laoreet in pharetra nec, vulputate vulputate urna. Vivamus quis mi ipsum, id pretium lorem. Ut eget magna lacus, a feugiat elit. Vestibulum blandit fringilla odio, et aliquam ipsum dapibus quis. <onions> Morbi euismod erat eget purus</oranges> viverra congue. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam eleifend volutpat ipsum at consectetur. Nullam nisi est, elementum vitae luctus sit amet, pulvinar vel lectus.

    I want it to be able to scan the entire file finds any patterns with <onions> and </oranges> delete them and everything in between. So in this scenario it would delete the following:
    <onions> Donec ac nunc turpis, ut accumsan turpis. In </oranges>
    <onions> Morbi euismod erat eget purus</oranges>

    Any ideas? :) Any help would be appreciated.
     
    samie, Jan 16, 2013 IP
  2. danmaster

    danmaster Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    Try it
    preg_match_all("#<onions[^>]*>(.+?)</oranges>#is", $string, $result);
    PHP:
     
    danmaster, Jan 16, 2013 IP
  3. sonzoy

    sonzoy Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    sonzoy, Jan 17, 2013 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    If your deleting text use
    str_replace()
    PHP:
    ...instead.
     
    MyVodaFone, Jan 17, 2013 IP