Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts

    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

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    $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.

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Are you using it in a loop or something? Post your code..

  • #5
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    It's not finding subject=" in the string, try echo($nsubject . '<br />'); before preg_match_all().

  • #7
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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

  • #8
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    sorted thanks again mate.

    Graham


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •