SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    having issues with eregi() to check if multi-word phrase is in string

    I have the following function that counts the # of times a phrase is in a string (string is a web page). It works great for single words (search), but it returns 0 when there is more than one keyword in the phrase (search engine optimization). What am I doing wrong? Is there a better way to do this?

    PHP Code:
    function CountQueryWords($string$query)
    {
        
    //count with html tags
        
    $count 0;
        
    $words explode(' '$string);
        for(
    $x=0$x count($words); $x++)
        {
            if (
    eregi($query$words[$x])) {
                
    $count $count 1;
            }
        }
        
        
    //count without html tags    
        
    $countAll 0;
        
    $words explode(' 'strip_tags($string));
        for(
    $x=0$x count($words); $x++)
        {
            if (
    eregi($query$words[$x])) {
                
    $countAll $countAll 1;
            }
        }    
        
    $queryCount = array("sourcecode" => $count"nosourcecode" => $countAll);
        return 
    $queryCount;


  2. #2
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure what the problem is (you didn't post any samples to test with) but foreach and stristr would be more efficient.
    PHP Code:
      function CountQueryWords($string$query)
      {
          
    //count with html tags
          
    $count 0;
          
    $words explode(' '$string);
          foreach(
    $words as $word)
          {
              if(
    stristr($word$query))
              {
                  
    $count++;
              }
          }
      
          
    //count without html tags
          
    $countAll 0;
          
    $words explode(' 'strip_tags($string));
          foreach(
    $words as $word)
          {
              if(
    stristr($word$query))
              {
                  
    $countAll++;
              }
          }
          
    $queryCount = array("sourcecode" => $count"nosourcecode" => $countAll);
          return 
    $queryCount;
      } 

  3. #3
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something using substr_count() might work also. Untested
    PHP Code:
     function CountQueryWords($string$query)
     {
         
    //count with html tags
         
    $count 0;
         
    $count substr_count($string$query);
     
         
    //count without html tags
         
    $countAll 0;
         
    $countAll substr_count(strip_tags($string), $query);
         
    $queryCount = array('sourcecode' => $count'nosourcecode' => $countAll);
         return 
    $queryCount;
     } 

  4. #4
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, I just realized why it's not working. I was exploding the string be each word so there would never be more than one word in each row of the array. I'm an idiot....


Bookmarks

Posting Permissions

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