regular expression

Discussion in 'PHP' started by ssimon171078, Nov 15, 2014.

  1. #1
    i need to receive number of pages in html page ,i started to write this code how to fix this code in php ?
    my html code is :
    <li class='current'><B>1</B> / 385</li><a href='website'  ,i need to receive number 385 in php
    <?php
    $website="websitex";
    
    $content=file_get_contents($website);
    $stripped_file = strip_tags($content, "<a><li>");
    //echo $stripped_file ."<br>";
    $start=strpos($stripped_file,'<li class');
    echo $start ."<br>":  
    ?>
    Code (markup):

     
    Last edited by a moderator: Nov 15, 2014
    ssimon171078, Nov 15, 2014 IP
  2. PunctRo

    PunctRo Active Member

    Messages:
    102
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    83
    #2
    Hi simon,

    Here is a snippet based on your example that should get you going :

    $content="<li class='current'><B>1</B> / 381</li><a href='website'  ,i need to receive number 385 in php";
    
    
    $startsAt = strpos($content, "<B>1</B> /") + strlen("<B>1</B> /");
    $endsAt = strpos($content, "</li><a href='website'", $startsAt);
    $result = substr($content, $startsAt, $endsAt - $startsAt);
    echo $result;
    PHP:
    No regular expression there.
    Hope it helps
     
    PunctRo, Nov 18, 2014 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Another way of doing it:

    
    $a = "<li class='current'><B>1</B> / 385</li><a href='website'";
    preg_match_all('/\d+/', $a, $res);
    echo "Showing: {$res[0][0]} To {$res[0][1]}";
    
    PHP:
    Assuming they are the first two numbers in the string.
     
    ThePHPMaster, Nov 19, 2014 IP