...

View Full Version : Counting preg match results



graham23s
08-27-2007, 10:56 PM
Hey Guys,

i was wondering if there was a way to count how many preg match results you get back

my code is:


$matchesfound = preg_match('/subject="/', $nsubject, $matchfound);

any help would be great

cheers

Graham

Inigoesdr
08-27-2007, 11:09 PM
$matchesfound will have either 0 or 1, since preg_match will only find one and then stop. If you use preg_match_all() then you would get all of the matches in the subject.

graham23s
08-27-2007, 11:21 PM
Hi Mate,

when i echo the variable out i get:


00000000000000000000000000000000000000000000000000000000000000000000


im wanting to count the number of 0's , iv even tried strlen but nothin

cheers

Graham

Inigoesdr
08-27-2007, 11:32 PM
Are you using it in a loop or something? Post your code..

graham23s
08-27-2007, 11:48 PM
Hi Mate,

yep its in a loop which returns the info i want when done , the number of 0's represent files which i was after the number of

code:


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++;
}

//=====================================================================================//
// Easy way to count number of files preg_match all the subject=" and e-voila :)
//=====================================================================================//

$matchesfound = preg_match_all('/subject="/', $nsubject, $matchfound);

echo $matchesfound;


}

thanks mate

Graham

Inigoesdr
08-28-2007, 12:00 AM
It's not finding subject=" in the string, try echo($nsubject . '<br />'); before preg_match_all().

graham23s
08-28-2007, 12:13 AM
ahh yeah thats true, i just realised i need to actually search in the .xml file rather than kust the subject (although the 0's and 1's were giving me back the amount of subject='s there was in the file

can a preg_match search in an xml file at all?

thanks mate

Graham

graham23s
08-28-2007, 12:16 AM
sorted thanks again mate.

Graham



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum