graham23s Posted August 28, 2007 Share Posted August 28, 2007 Hi Guys, what im trying to do here is calculate file sizes in an .xml file for example this is the bit i need to calculate: <segments> <segment number="1" bytes="259072">DPydnTk-mLYWEU_bnZ2dnUVZ_s6mnZ2d@giganews.com</segment> <segment number="2" bytes="259072">DPydnTg-mLYWEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="3" bytes="259072">DPydnTs-mLYREU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="4" bytes="259072">DPydnTo-mLYREU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="5" bytes="259072">DPydnTU-mLYQEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="6" bytes="259072">DPydnTQ-mLYQEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="7" bytes="259072">DPydnTc-mLYTEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="8" bytes="259072">DPydnTY-mLYTEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="9" bytes="259072">DPydnTE-mLYSEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="10" bytes="259072">DPydnTA-mLYNEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="11" bytes="259072">DPydnTM-mLYNEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="12" bytes="259072">DPydnTI-mLYNEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="13" bytes="259072">DPydnS0-mLYMEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="14" bytes="259072">DPydnSw-mLYMEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="15" bytes="259072">DPydnS8-mLYPEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="16" bytes="259072">DPydnS4-mLYPEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="17" bytes="259072">DPydnSk-mLYPEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="18" bytes="259072">DPydnSg-mLYOEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="19" bytes="259072">DPydnSs-mLYOEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="20" bytes="259072">DPydnSo-mLYJEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="21" bytes="259072">DPydnSU-mLYJEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="22" bytes="259072">DPydnSQ-mLYIEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="23" bytes="259072">DPydnSc-mLYIEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="24" bytes="259072">DPydnSY-mLYIEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="25" bytes="259072">DPydnSE-mLYLEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="26" bytes="259072">DPydnSA-mLYLEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="27" bytes="259072">DPydnSM-mLYKEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="28" bytes="259072">DPydnSI-mLYKEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="29" bytes="259072">DPydnV0-mLYKEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="30" bytes="259072">DPydnVw-mLYFEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="31" bytes="259072">DPydnV8-mLYFEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="32" bytes="259072">DPydnV4-mLYEEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="33" bytes="259072">DPydnVk-mLYEEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="34" bytes="259072">DPydnVg-mLYHEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="35" bytes="259072">DPydnVs-mLYHEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="36" bytes="259072">DPydnVo-mLYHEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="37" bytes="259072">DPydnVU-mLYGEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="38" bytes="259072">DPydnVQ-mLYGEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="39" bytes="259072">DPydnVc-mLYBEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="40" bytes="259072">DPydnVY-mLYBEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="41" bytes="259072">DPydnVE-mLYAEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="42" bytes="259072">DPydnVA-mLYAEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="43" bytes="259072">DPydnVM-mLYDEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="44" bytes="259072">DPydnVI-mLYDEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="45" bytes="259072">DPydnU0-mLYDEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="46" bytes="259072">DPydnUw-mLYCEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="47" bytes="259072">DPydnU8-mLYCEU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="48" bytes="259072">DPydnU4-mLY9EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="49" bytes="259072">DPydnUk-mLY9EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="50" bytes="259072">DPydnUg-mLY8EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="51" bytes="259072">DPydnUs-mLY8EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="52" bytes="259072">DPydnUo-mLY8EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="53" bytes="259072">DPydnUU-mLY_EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="54" bytes="259072">DPydnUQ-mLY_EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="55" bytes="259072">DPydnUc-mLY-EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="56" bytes="259072">DPydnUY-mLY-EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="57" bytes="259072">DPydnUE-mLY5EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="58" bytes="259072">DPydnUA-mLY5EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="59" bytes="259072">DPydnUM-mLY4EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="60" bytes="259072">DPydnUI-mLY4EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> <segment number="61" bytes="25600">DPydnX0-mLY7EU_bnZ2dnUVZ_s7inZ2d@giganews.com</segment> </segments> using a preg_match_all i can count the number of 'bytes=' but is there a way i can extract the value of bytes, then i can times it by the number of preg_match_all results or is there a better way? thanks guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/67009-getting-file-sizes/ Share on other sites More sharing options...
ReDucTor Posted August 28, 2007 Share Posted August 28, 2007 Simple XML $xml = simplexml_load_file('file.xml'); $size = 0; foreach($xml->segments->segment as $segment) $size += $segment['bytes']; DOM XML $xml = domxml_open_file('file.xml'); $segments = $xml->getElementsByTagName('segments')->item(0); $segments = $segments->getElementsByTagName('segment'); $size = 0; foreach($segments as $segment) $size += $segment->getAttribute('bytes'); Quote Link to comment https://forums.phpfreaks.com/topic/67009-getting-file-sizes/#findComment-336003 Share on other sites More sharing options...
graham23s Posted August 28, 2007 Author Share Posted August 28, 2007 Hi Mate, thanks for that, this is the code for reading the .xml: $filecontent = file_get_contents("nzbs/$renamed_nzb"); $xml = simplexml_load_string($filecontent); $nzbpiece = 0; foreach($xml->{"file"} as $nfile) { $nposter = (string) trim($nfile['poster']); $nposter = ($nposter); $ndate = 0 + trim($nfile['date']); $nsubject = (string) trim($nfile['subject']); // To find num of segments in subject: $nsubjsegs = 0 + subj_seg($nsubject); // To find out if Par or not $npar = (stristr($nsubject, "par2")?0:1); $nsubject = ($nsubject); $groups = array(); foreach($nfile->groups->group as $group) { $groups[] = (string) trim($group); } $ngroups = (serialize($groups)); //$nsegcount = 0; //$nsize = 0; //foreach($nfile->segments->segment as $segment) { // $nbytes = 0 + trim($segment['bytes']); // $nsize += $nbytes; // $nsegcount++; //} } $size = 0; foreach($xml->segments->segment as $segment) $size += $segment['bytes']; echo $size; $matchesfound = preg_match_all('/subject="/', $filecontent, $matchfound); ## strtolower the files incase some par2's are uppercase $parsfound = preg_match_all("/.par2/", strtolower($filecontent), $parfind); when i do the code above it produces a 0 instead of the actual amount have i implemented it ok? cheers Graham Quote Link to comment https://forums.phpfreaks.com/topic/67009-getting-file-sizes/#findComment-336148 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.