Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-27-2007, 09:56 PM   PM User | #1
graham23s
Regular Coder

 
Join Date: Jul 2007
Location: Scotland
Posts: 134
Thanks: 12
Thanked 0 Times in 0 Posts
graham23s is an unknown quantity at this point
Counting preg match results

Hey Guys,

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

my code is:

PHP Code:
$matchesfound preg_match('/subject="/'$nsubject$matchfound); 
any help would be great

cheers

Graham
graham23s is offline   Reply With Quote
Old 08-27-2007, 10:09 PM   PM User | #2
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
$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.
Inigoesdr is offline   Reply With Quote
Old 08-27-2007, 10:21 PM   PM User | #3
graham23s
Regular Coder

 
Join Date: Jul 2007
Location: Scotland
Posts: 134
Thanks: 12
Thanked 0 Times in 0 Posts
graham23s is an unknown quantity at this point
Hi Mate,

when i echo the variable out i get:

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

cheers

Graham
graham23s is offline   Reply With Quote
Old 08-27-2007, 10:32 PM   PM User | #4
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Are you using it in a loop or something? Post your code..
Inigoesdr is offline   Reply With Quote
Old 08-27-2007, 10:48 PM   PM User | #5
graham23s
Regular Coder

 
Join Date: Jul 2007
Location: Scotland
Posts: 134
Thanks: 12
Thanked 0 Times in 0 Posts
graham23s is an unknown quantity at this point
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:

PHP Code:
     foreach($xml->{"file"} as $nfile) {
     
        
$nposter = (string) trim($nfile['poster']);
        
$nposter = ($nposter);
                    
        
$ndate trim($nfile['date']);
                    
        
$nsubject = (string) trim($nfile['subject']);
        
// To find num of segments in subject:
        
$nsubjsegs 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 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
graham23s is offline   Reply With Quote
Old 08-27-2007, 11:00 PM   PM User | #6
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
It's not finding subject=" in the string, try echo($nsubject . '<br />'); before preg_match_all().
Inigoesdr is offline   Reply With Quote
Old 08-27-2007, 11:13 PM   PM User | #7
graham23s
Regular Coder

 
Join Date: Jul 2007
Location: Scotland
Posts: 134
Thanks: 12
Thanked 0 Times in 0 Posts
graham23s is an unknown quantity at this point
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 is offline   Reply With Quote
Old 08-27-2007, 11:16 PM   PM User | #8
graham23s
Regular Coder

 
Join Date: Jul 2007
Location: Scotland
Posts: 134
Thanks: 12
Thanked 0 Times in 0 Posts
graham23s is an unknown quantity at this point
sorted thanks again mate.

Graham
graham23s is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:28 AM.


Advertisement
Log in to turn off these ads.