Jump to content

[SOLVED] Previous Question - Regular Expressions


OLG

Recommended Posts

Hey,

Approximately 2 years ago i asked a question on regular expressions, the topic is therefore archaeic and unrecoverable but contained the info i needed to solve my current problem.

 

<?php
$mycookies = GetCookies("www.site.com/login.php",
"usrname=user&password=pass&[email protected]&submit=LOGIN&set=Y","");
$opts = @array('http'=>array('method'=>"GET",
'header'=>"Accept-language: en\r\nCookie: ".$mycookies."\r\n" ));
$context = stream_context_create($opts);
$fp = fopen('http://www.site.com/statistics.php?id=2111292', 'r', false, $context);
fpassthru($fp);
$html = fread($fp, 1000000);
fclose($fp);
$strip = strip_tags($html);
echo $strip;

function PostPage($host,$query,$others=''){
   $path=explode('/',$host);
   $host=$path[0];
   unset($path[0]);
   $path='/'.(implode('/',$path));
$post="POST $path HTTP/1.1\r\nHost: $host\r\n";
$post.="Content-type: application/x-www-form-";
$post.="urlencoded\r\n${others}";
$post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
$post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
   $h=fsockopen($host,80);
   fwrite($h,$post);
   for($a=0,$r='';!$a;){
       $b=fread($h,8192);
       $r.=$b;
       $a=(($b=='')?1:0);
   }
   fclose($h);
   return $r;
}
function GetCookies($host,$query,$others=''){
   $path=explode('/',$host);
   $host=$path[0];
   unset($path[0]);
   $crlf = "\r\n";
   $path='/'.(implode('/',$path));
   $post="POST $path HTTP/1.1\r\nHost: $host\r\n";
$post.="Content-type: application/x-www-form-urlencoded\r\n${others}";
$post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
$post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
   $h=fsockopen($host,80);
   fwrite($h,$post);
   $r=""; 
   for($a=0;!$a;){
       $b=fread($h,512);
       echo $b;
       $r.=$b;
       $gotSession=strpos($r,"ASPSESSION");
   if($gotSession)
      if(strpos($r, $crlf . $crlf,$gotSession)>0) break;
       $a=(($b=='')?1:0);
   }
   fclose($h);
   $arr = split("Set-Cookie:",$r);
   $AllCookies="";$count=1;
   while ($count < count($arr)) {
$AllCookies.=substr($arr[$count].";",
0,strpos($arr[$count].";",";")+1);

$count++;}
   return $AllCookies;

}
?>

 

This is my current script - if i just run down what im doing here. I'm using fsock to establish a connection to a third party site and then submitting a set of details in order to login - i then strip the html tags away from the page that i've fopened and read.

 

What i now need is to use preg_match to extract the string "Rank: 999,999" for example and then split that down to 999,999 and then remove the comma - this is the part i am stuck on - i just really don't understand regular expressions.

 

Does anyone think they could help me out here and then explain it?

 

Thank You  ;D

Hi OLG. Neat scrape.

 

I'm not so good with RegEx either, but you can probably get what you want with explode() alone.

// after you've assign $html
$x= explode('Rank: ',$html,2);
if($x[1]) $y = explode(' ',$x[1],2);
$rank = str_replace(',','',$y[0]);

if(is_numeric($rank)) echo $rank;
else echo 'this didnt work after all..';

I don't think that worked

 

Additionally - i have now fixed the stripping issue (stupidly used fpassthru - now sorted) - however there is still some javascript in the result - so i need to remove that.

 

I'm fairly certain that it didn't - any other ideas?

 

Thanks for the help so far (that was very fast).

 

 

Adam

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.