SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Preg_Match Question

    I am tired of searching. I have a string "<!-- Top 5 Leaderboard --><div id="leaderboard">Top 5 Leaderboard</div><!-- /Top 5 Leaderboard -->"

    I would like to extract contents between "<!-- Top 5 Leaderboard -->" and "<!-- /Top 5 Leaderboard -->"

    Can anyone help me What pattern should I use?

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    Hi,

    Parsing HTML with a regular expression is almost never a good idea.

    That said, try this:

    PHP Code:
    $matches null;
    $returnValue preg_match('/-->(.*)<!--/''<!-- Top 5 Leaderboard --><div id="leaderboard">Top 5 Leaderboard</div><!-- /Top 5 Leaderboard -->'$matches);
    print_r($matches); 

  3. #3
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just changed a little bit to suit my requirements but getting error.

    PHP Code:
    $source ='<!-- Top 5 Leaderboard --><div id="leaderboard">Top 5 Leaderboard adfasfadf ILX (New for 2013!)
    Overview: The least expensive route to Hondas premium-label goodness
    Drivetrain: Two four-cylinder engines to choose from as well as a gas-electric hybrid front-wheel-drive only
    </div><!-- /Top 5 Leaderboard -->'
    ;
    $matches null;$returnValue preg_match('/<!-- Top 5 Leaderboard(.*)</div><!--/'$source$matches);print_r($matches); 
    Quote Originally Posted by Pullo View Post
    Hi,

    Parsing HTML with a regular expression is almost never a good idea.

    That said, try this:

    PHP Code:
    $matches null;
    $returnValue preg_match('/-->(.*)<!--/''<!-- Top 5 Leaderboard --><div id="leaderboard">Top 5 Leaderboard</div><!-- /Top 5 Leaderboard -->'$matches);
    print_r($matches); 

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    Yo need to escape the slash in the closing </div> tag and make sure that the string doesn't contain any line breaks (or use the multi-line modifier).

    E.g.

    PHP Code:
    /<!-- Top 5 Leaderboard(.*)<\/div><!--/ 

  5. #5
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Already tried but receiving error.

    Warning: preg_match(): Unknown modifier '<' in C:\xampp\htdocs\test.php on line xx




    Quote Originally Posted by Pullo View Post
    Yo need to escape the slash in the closing </div> tag and make sure that the string doesn't contain any line breaks (or use the multi-line modifier).

    E.g.

    PHP Code:
    /<!-- Top 5 Leaderboard(.*)<\/div><!--/ 

  6. #6
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is my final code but same error

    $source ='<!-- Top 5 Leaderboard --><div id="leaderboard">adfasfadf ILX (New for 2013!)


    Overview: The least expensive route to Hondas premium-label goodness


    Drivetrain: Two four-cylinder engines to choose from as well as a gas-electric hybrid front-wheel-drive only


    </div><!-- /Top 5 Leaderboard -->';


    $matches = null;
    $returnValue = preg_match('<!-- Top 5 Leaderboard -->(.*)<\/div><!/', $source, $matches);
    //$returnValue = preg_match('<!-- Top 5 Leaderboard -->(.*)<!-- \/Top 5 Leaderboard -->/', $source, $matches);
    print_r($matches);

  7. #7
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    Hi there,

    Try this:

    PHP Code:
    <?php
    $source 
    ='<!-- Top 5 Leaderboard --><div id="leaderboard">adfasfadf ILX (New for 2013!)


    Overview: The least expensive route to Hondas premium-label goodness


    Drivetrain: Two four-cylinder engines to choose from as well as a gas-electric hybrid front-wheel-drive only


    </div><!-- /Top 5 Leaderboard -->'
    ;


    $pattern '/<!-- Top 5 Leaderboard -->(.*)<!-- \/Top 5 Leaderboard -->/s';
    preg_match($pattern$source$matches);
    echo 
    htmlspecialchars($matches[1]);
    ?>
    By default the fullstop doesn't match a newline character and you have to use the s modifier to force it to.

  8. #8
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it worked.

    Quote Originally Posted by Pullo View Post
    Hi there,

    Try this:

    PHP Code:
    <?php
    $source 
    ='<!-- Top 5 Leaderboard --><div id="leaderboard">adfasfadf ILX (New for 2013!)


    Overview: The least expensive route to Hondas premium-label goodness


    Drivetrain: Two four-cylinder engines to choose from as well as a gas-electric hybrid front-wheel-drive only


    </div><!-- /Top 5 Leaderboard -->'
    ;


    $pattern '/<!-- Top 5 Leaderboard -->(.*)<!-- \/Top 5 Leaderboard -->/s';
    preg_match($pattern$source$matches);
    echo 
    htmlspecialchars($matches[1]);
    ?>
    By default the fullstop doesn't match a newline character and you have to use the s modifier to force it to.


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
  •