Hi guys, I have a HTML string which I need to parse for certain info. I have looked at preg_match but I can tget it to do what I need. I have also looked at this function http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/ but it only works with single sets of tags.

Here is my string:

$html = '<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title>GPRS Module Status</title>
<link rel="stylesheet" href="/style.htm" type="text/css">
</head>
<body>
<form name=emodstat action="/goform/emodstat" method=POST>
<input type=hidden name="act" value=''><H1>Results of Last Module Status Poll:</H1><H1>Outcome: Got modem status OK:</H1><H1>Time: 3 Jan 2000 20:43:35</H1><TABLE border=1>
<TR><TD>SIM status</TD><TD> READY</TD></TR><TR><TD>Signal strength</TD><TD>-81 dBm</TD></TR><TR><TD>Manufacturer</TD><TD>Novatel Wireless Incorporated</TD></TR><TR><TD>Model</TD><TD>Expedite EU870D MiniCard</TD></TR><TR><TD>IMEI</TD><TD>000000000000000</TD></TR><TR><TD>IMSI</TD><TD>234100500000000</TD></TR><TR><TD>ICCID</TD><TD>0000000500000000000</TD></TR><TR><TD>Firmware</TD><TD>10.9.00.0-00  [2007-02-09 15:06:02]</TD></TR><TR><TD>GPRS Attachment Status</TD><TD>Attached</TD></TR><TR><TD>GPRS Registration</TD><TD>Registered, home network</TD></TR><TR><TD>GSM Registration</TD><TD>Registered, home network</TD></TR><TR><TD>Network</TD><TD> 0,0,"SomeNet,0</TD></TR><TR><TD>Service Provider Name</TD><TD> "SomeNet"</TD></TR><TR><TD>Radio Access Technology</TD><TD>Auto,CS+PS,GSM CS+PS</TD></TR><TR><TD>Network Technology</TD><TD>EDGE</TD></TR><TR><TD>Cell Information</TD><TD> lac:0007 ci:1514</TD></TR><TR><TD>Connection Status</TD><TD> 0</TD></TR></TABLE>
<p>Click the refresh button to get the current status.</p><input type=submit name=state value="Refresh">
<p>Click the scan button to scan for available networks.</p><input type=submit name=state value="Scan">
<p>Click the auto button to unlock all network preferences.</p><input type=submit name=pbut value="Auto" onClick="emodstat.act.value=auto">
</form>
</body>
</html>';

What I need it to be able to get the variables:

// Signal Strength
$signal = "-81";
// GPRS Attachment Status
$gprs_attach_status = "Attached";
// GPRS Registration
$gprs_reg = "Registered, home network";
// Service Provider Name
$provider_name = "SomeNet";

Thanks for any help...

    Hi.
    There are several ways to do these REGEX regular expressions.
    Some will be more successful if the HTML page layout eventually will change.
    Other REGEX will fail if the HTML page change, and so the REGEX will have to be modified.

    I think my code below is reasonably good. And still not too complicated.
    Of course there are possible additions to this code to improve it.
    I have tested it at my PHP development webserver. And it works.
    With the HTML example you posted.

    <?php
    
    /*
    // Signal Strength
    $signal = "-81";
    // GPRS Attachment Status
    $gprs_attach_status = "Attached";
    // GPRS Registration
    $gprs_reg = "Registered, home network";
    // Service Provider Name
    $provider_name = "SomeNet"; 
    */
    
    $signal_find = '#(-{0,1}\d+) *dBm#siU';
    preg_match($signal_find, $html, $match);
    $signal = trim($match[1]);
    echo $signal;
    
    echo "<br />\n";
    
    $attach_find = '#Attachment Status</TD><TD>(.*)<#siU';
    preg_match($attach_find, $html, $match);
    $gprs_attach_status = trim($match[1]);
    echo $gprs_attach_status;
    
    echo "<br />\n";
    
    $reg_find = '#GSM Registration</TD><TD>(.*)<#siU';
    preg_match($reg_find, $html, $match);
    $gprs_reg = trim($match[1]);
    echo $gprs_reg;
    
    echo "<br />\n";
    
    $provider_find = '#Provider Name</TD><TD>(.*)<#siU';
    preg_match($provider_find, $html, $match);
    $provider_name = trim($match[1], ' "');
    echo $provider_name;
    
    ?>

      If you have a full HTML document like you illustrate, more robust parsing is available through [man]DOM[/man].

      $report = new DOMDocument();
      $report->loadHTML($html);
      $query = new DOMXPath($report);
      $table = $query->evaluate('//table')->item(0);
      
      $signal             = $query->evaluate('string(tr[2]/td[2])', $table);
      $gprs_attach_status = $query->evaluate('string(tr[9]/td[2])', $table);
      $gprs_reg           = $query->evaluate('string(tr[11]/td[2])', $table);
      $provider_name      = $query->evaluate('string(tr[13]/td[2])', $table);
      
        Write a Reply...