quick regex help

Discussion in 'PHP' started by Aces A, Jul 27, 2009.

  1. #1
    I have an html page that I loaded into a variable ($page) via cURL, and I need to extract a piece of information from it.

    There is a tag such as:
    <h2 class="yxz">MY TEXT</h2>

    And I want to extract the 'my text' inside the tag.

    There is only going to be one of such tags on the page, so I used strpos to get the location of the opening tag, and then str_replace to cut everything out before the tag. But the amount of text inside the tag is a variable length, and I am having trouble extracting it.

    I now have a string ($str) that looks like:
    SOME TEXT HERE </h2> ....

    I tried using the regex: /[^<]+/ (greedily select any number of characters that isn't a '<' ) with preg_split to get everything before the </h2>, but I can't seem to get it working.

    Thanks.

    And I realize that this can be done without a regular expression, but I need to learn how to work regex within php.
     
    Aces A, Jul 27, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Is this always going to be <h2 class="yxz">?

    Or can the class name change?
     
    jestep, Jul 27, 2009 IP
  3. Martinoes

    Martinoes Peon

    Messages:
    110
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Use this pattern to find any <h2> tag

    <?php
    $pattern = "(<h2[^>]*?>([^<]*?)</h2>)";
    $valid = preg_match($pattern, $data, $SEARCH);
    
    if ($valid) {
    	echo $SEARCH[1];
    }
    ?>
    PHP:
    .. and this pattern for specific h2.class name. (eg. yxz)

    <?php
    $pattern = "(<h2[^>]*?class=\"yxz\">([^<]*?)</h2>)";
    $valid = preg_match($pattern, $data, $SEARCH);
    
    if ($valid) {
    	echo $SEARCH[1];
    }
    ?>
    PHP:
     
    Martinoes, Jul 29, 2009 IP