Search code examples
phphtmlpreg-match

preg_match html table match


Here's my PHP Code, I am currently having:

function getStatus($file, $dob){
    $url = "http://www.passportindia.gov.in/AppOnlineProject/statusTracker/trackStatusForFileNoNew?fileNo=$file&applDob=$dob";
    $data = file_get_contents($url);

    echo preg_match('/^<table cellpadding=\"4\" cellspacing=\"4\" align=\"center\" width=\"100%\" role=\"presentation\">(.*)<\/table>/',$data, $converted);

    //$converted = preg_replace("/[^0-9.]/", "", $converted[1]);
    //return round($converted, 3); 
}

I would like to fetch all content between <table cellpadding="4" cellspacing="4" align="center" width="100%" role="presentation"> and </table>

Currently preg_match returns 0.

Your support will be highly appreciated.


Solution

  • This is easy. Your regular expression (preg_match) starts with a caret icon: ^

    This means it should look for your expression at the beginning of the string. Just remove the caret:

    preg_match('/<table cellpadding=\"4\" cellspacing=\"4\" align=\"center\" width=\"100%\" role=\"presentation\">(.*)<\/table>/',$data, $converted);
    

    Now it should look everywhere in the HTML file.

    Some food for thought

    For debugging it would be wise to make sure that the content of the $data variable is what you are looking for. Since you are referring to an external URL you never know which exact HTML structure/file content you get back. Also the response might change without you being aware of it (for example: maybe 1 week ago the cellspacing=\"4\" was present but now is not any longer. If you really have to do it that way write code that expects the response is not what you want and throws an adequate error.

    When I look at the HTML file you are reading I do not see the search term anywhere so I would assume even with the corrected regex you will still get 0 as result.

    To make your life easier

    A nice tool to check if your regex is working or not is this one: http://regexr.com/

    I used it a lot until I finally understood regular expressions (and still do).