I've been trying to figure this one out for some time. I'm using a rather hacked to death version of a script I found on cputils.com to show disk usage for all accounts on a WebHost Manager list that I intend to use within a backup schedule script. It uses the preg_match_all expression, construct, command, not sure really, anyway here's the code if somebody can give me a hint as to why mine isnt working :quiet:
This is my code:

//get WHM list accounts
$page2 = @file_get_contents("http://$whmUser:$whmPass@$url:2086/scripts2/listaccts?viewall=1");

//get each account table row
$match2 = preg_match_all("|<[tr class=\"tdshade2\"]>(.*?)<[/tr]>|U", $page2, $match);

if(isset($match2)) {
foreach($match as $stuff => $match3) {
$account = explode('</td><td>',$stuff);
$msg .= "<br>".'Domain: '.strip_tags($account[0]).'<br>'.' -- Space used: '.$account[54]."\n";
echo $msg;
}
echo "<br>"."True but loop not working...";
}
else {
$msg .= "\n<br>Error<br>\n";
echo $msg;
}


Now the original:

$page2 = @file_get_contents("https://$whmUser:$whmPass@$url:2087/scripts2/listaccts?viewall=1");

//get each accounts table row
$int2 = preg_match_all("/<tr class=(?:tdshade2|tdshade1)>(.*?)<\/tr>/is", $page2, $matches);

if($int2 > 0 && is_array($matches[1])) {
foreach($matches[1] as $match) {

$account = explode('</td><td>',$match);
$msg .= 'Domain: '.strip_tags($account[0]).' -- Space used: '.$account[7]."\n";
$clientTotal += $account[7];
}
$total = ceil($clientTotal + $resellerTotal[1]);
$msg .= 'Total space used: '.$total." mb \n";

$remaining = ceil($resellerQuota - $total);
$msg .= 'Remaining space: '.$remaining." mb \n";

}
else {
$msg .= "No workee.\n";
}

Here is something a friend did get to work however:
$page = @file_get_contents('http://$whmUser:$whmPass@$url:2086/scripts2/listaccts?viewall=1','r');
$int = preg_match_all("|<[>]+>(.*)</[>]+>|U", $page, $match);
if(isset ($int)) {
foreach($match as $stuff => $matches){
$match[$stuff] = strip_tags($match[$stuff]);
echo "<HR>";
echo $match[$stuff];
echo "<HR>";
print_r($match);
echo "<HR>".$page;
}
}
else {
echo 'no data';
}

So the hacked up version (mine) of his code doesnt seem to work either and I'm guessing the reason the friend's works is because it searches for anything correct? I've looked through php.net's man pages on preg_match and preg_match_all. I'm guessing the problem must lie in the string I am searching for or how I am formatting the search, maybe his code was deprecated to begin with? I've only about 2 months experience with PHP, so if anybody can help, please be gentle 🙂

    Looks like you're just trying to match a certain pattern of HTML code that might contain data you want.

    To diagnose this, we would probably need to see the contents in the $page2 variable (only an applicable snippet of it!) as well as what it is you're trying to display.

      I tried to omit as much as possible without losing the main structure of what I need to pull from, all I'm really needing is the account name and the disk usage for said account name at that time. If need be I could post a link to the original code on cputils.com or just paste it in here, I'm not sure if its something I'm doing or if its something wrong with their script because I'm very new to the whole regular expression thing.
      Not sure what the tags are for html code in here but I'll try a few lol:

          
      <tr class="tdshade1"> <td><a href="http://yoursite2.com" target="_blank">yoursite2.com</a></td> <td align="center"><a href="/xfercpanel/username2" target="_blank"><img src="/cpiconb.gif" border="0"></a></td> <td><a href="http://74.52.000.000/~username2/">74.52.000.000</a></td> <td>username2</td> <td><a href="mailto:contact@yoursite2.com">contact@yoursite2.com</a>&nbsp;<a href="/scripts2/changeemail?domain=yoursite2.com&user=username2"> <img src="/change.gif" border="0"></a></td> <td align="center">07 Feb 12 8:51</td> <td>home</td> //this is the disk quota for the account// <td align="right">30&nbsp;M&nbsp;<a href="/scripts/quotalist?domain=yoursite2.com&user=username2"><img src="/change.gif" border="0"></a></td> //this is the line I am wanting specifically, disk space used// <td align="right">0&nbsp;M</td> <td align="center">package_name</a></td> <td align="center">rvblue</td> <td>resellername</td> </tr> <tr class="tdshade2"> <td><a href="http://username.com" target="_blank">yoursite.com</a></td> <td align="center"><a href="/xfercpanel/username" target="_blank"><img src="/cpiconb.gif" border="0"></a></td> <td><a href="http://74.52.000.000/~username/">74.52.000.000</a></td> <td>username</td> <td><a href="mailto:contact@yoursite.com">contact@yoursite.com</a>&nbsp;<a href="/scripts2/changeemail?domain=yoursite.com&user=username"> <img src="/change.gif" border="0"></a></td> <td align="center">07 Feb 16 22:53</td> <td>home</td> //this is the disk quota for the account// <td align="right">30&nbsp;M&nbsp;<a href="/scripts/quotalist?domain=yoursite.com&user=username"><img src="/change.gif" border="0"></a></td> //this is the line I am wanting specifically, disk space used// <td align="right">3&nbsp;M</td> <td align="center">package_name</td> <td align="center">rvblue</td> <td>resellername</td> </tr>

        One of the problems is that the dot will not match line breaks unless you set the s-modifier as in the original.

        If you want to match "ungreedy" which seems very likely to me, it is also kind of counterproductive to use the U modifier in conjunction with .*?, as this actually results in greedy behaviour again.

          Write a Reply...