Regex with HTML in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • namanhvu
    New Member
    • Feb 2008
    • 1

    #1

    Regex with HTML in PHP

    Hi,

    I have the following html code and need to display all contents within the <h4> tags, except the one containing "Footer". I am writing a parser script in php to transform an old to a new html layout.

    The old layout is basically like this:

    <h4>Backgroun d</h4>
    <h4>Advice</h4>
    <h4>Footer</h4>
    <h4>Support</h4>

    And my regex looks like this:

    $regex_newsecti ons = "/<h4>(.*?)(?=< \/h4>)/m";

    I need the brackets surrounding .*? because I will use the contents later on in the preg_match_all function.

    I have tried using a negative lookahead but none of the following work:
    $regex_newsecti ons = "/<h4>(.*?)(?!Foo ter)(?=<\/h5>)/m";
    $regex_newsecti ons = "/<h4>((?!Footer) .*?)(?=<\/h5>)/m";
    $regex_newsecti ons = "/<h4>((?!Footer) *?)(?=<\/h5>)/m";

    Does anyone have any ideas? Cheers.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Originally posted by namanhvu
    Hi,

    I have the following html code and need to display all contents within the <h4> tags, except the one containing "Footer". I am writing a parser script in php to transform an old to a new html layout.

    The old layout is basically like this:

    <h4>Backgroun d</h4>
    <h4>Advice</h4>
    <h4>Footer</h4>
    <h4>Support</h4>

    And my regex looks like this:

    $regex_newsecti ons = "/<h4>(.*?)(?=< \/h4>)/m";

    I need the brackets surrounding .*? because I will use the contents later on in the preg_match_all function.

    I have tried using a negative lookahead but none of the following work:
    $regex_newsecti ons = "/<h4>(.*?)(?!Foo ter)(?=<\/h5>)/m";
    $regex_newsecti ons = "/<h4>((?!Footer) .*?)(?=<\/h5>)/m";
    $regex_newsecti ons = "/<h4>((?!Footer) *?)(?=<\/h5>)/m";

    Does anyone have any ideas? Cheers.
    To start with the 3 instructions beginning at:
    [php]
    $regex_newsecti ons = "/<h4>(.*?)(?!Foo ter)(?=<\/h5>)/m";
    [/php]
    all search for </h5> as the closing tag.

    Ronald

    Comment

    Working...