1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Trying to use the PHP substr_count and getting an error

Discussion in 'XenForo Development Discussions' started by AndyB, Sep 29, 2013.

  1. AndyB

    AndyB Well-Known Member

    I'm creating an add-on where I would like to run a block of code like this:

    PHP:
        foreach ($smilietext as $substring)
         {
           
    $smilie_count += substr_count($input['message'], $substring);
         }
    The variable $smilietext is an array which contains the smilie_text of all the smiles in the xf_smilie table.

    The error I get is the following:

    Code:
    The following error occurred:
    substr_count() expects parameter 2 to be string, array given
    
      XenForo_Application::handlePhpError()
      substr_count() in Andy/SmilieCount/ControllerPublic/Post.php at line 63
      Andy_SmilieCount_ControllerPublic_Post->actionSave() in XenForo/FrontController.php at line 337
      XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 134
      XenForo_FrontController->run() in /home/*/www/forums/index.php at line 13
    
    No matter how I try to declare parameter 2 as a string I still get the error message.
     
  2. euantor

    euantor Well-Known Member

    Tried var_dumping $substring?
     
  3. AndyB

    AndyB Well-Known Member

    Hi euantor,

    When I add the following line of code:

    PHP:
        foreach ($smilietext as $substring)
         {
           return 
    $this->responseError($substring);
           
    $smilie_count += substr_count($input['message'], $substring);
         }
    I get the first smile text as I'm expecting. Here's a screen print:

    pic001.jpg
     
  4. euantor

    euantor Well-Known Member

    Strange. Have you tried casting $substring as a string explicitly:

    PHP:
    foreach ($smilietext as $substring) {
        
    $smilie_count += substr_count($input['message'], (string) $substring);
    }
     
  5. AndyB

    AndyB Well-Known Member

    I tried that:

    PHP:
    $smilie_count 0;
    foreach (
    $smilietext as $substring
    {
      
    $smilie_count += substr_count($input['message'], (string) $substring);
    }
    return 
    $this->responseError($smilie_count);

    but the smile count is always at 0 no matter how many smilies I have in the post.
     
  6. euantor

    euantor Well-Known Member

    Before the foreach loop, could you add var_dump($smilietext) and post the output? That way we can see what's in the variable in case it's not a flat array or something else is going wrong.
     
  7. AndyB

    AndyB Well-Known Member

    The only way I know how to get an output is to use the:

    return $this->responseError($variable);

    Unfortunately this does not work:

    return $this->responseError(var_dump($smilietext));
     
  8. AndyB

    AndyB Well-Known Member

    Here's how I get $smilietext

    PHP:
        // get smilie list array (ie: :) ;) :( :mad: :confused: :cool: :p :D :eek: :oops: etc...)   
         
    $smilietext $this->getModelFromCache('XenForo_Model_Smilie')->getSmilieText();
     
  9. AndyB

    AndyB Well-Known Member

    Here's the Smilie.php code:

    PHP:
    <?php

    class Andy_SmilieCount_Model_Smilie extends XFCP_Andy_SmilieCount_Model_Smilie
    {
       public function 
    getSmilieText()
       {
         return 
    $this->_getDb()->fetchAll('
         SELECT smilie_text
         FROM xf_smilie'
    );
       }
    }

    ?>
     
  10. euantor

    euantor Well-Known Member

    If you simply add the code, it should output the whole array. I'm new to XF development, but it should work.
     
  11. AndyB

    AndyB Well-Known Member

    You mean add the var_dump($smilietext) code? No that won't work, I get a generic error message asking me to look in the javascript console.
     
  12. euantor

    euantor Well-Known Member

    Strange. What happens if you echo $substring?

    PHP:
    foreach ($smilietext as $substring) {
        echo 
    $substring "<br />";
        
    //$smilie_count += substr_count($input['message'], (string) $substring);
    }
    You should see a list of all the substrings then which might help work out what's going wrong.
     
  13. AndyB

    AndyB Well-Known Member

    Unfortunately the echo command will not output as expected in some parts of the code.
     
  14. tyteen4a03

    tyteen4a03 Well-Known Member

    The var_dump() should appear in the JavaScript console then.
     
  15. AndyB

    AndyB Well-Known Member

    The var_dump($smilietext) worked when I put it right after the the code that populated $smilietext.

    Here is the portion of the dump.

    Code:
    array(43) { [0]=> array(1) { ["smilie_text"]=> string(7) ":2cents" } [1]=> array(1) { ["smilie_text"]=> string(6) ":angel" } [2]=> array(1) { ["smilie_text"]=> string(6) ":angry" } [3]=> array(1) { ["smilie_text"]=> string(5) ":bash" } [4]=> array(1) { ["smilie_text"]=> string(8) ":biggrin" } [5]=> array(1) { ["smilie_text"]=> string(7) ":bounce" } [6]=> array(1) 
    
     
  16. AndyB

    AndyB Well-Known Member

    Looks like "smilie_text" is the key.
     
  17. euantor

    euantor Well-Known Member

    Yes, that's the issue. You're actually getting an array in $substring.
     
    AndyB likes this.
  18. AndyB

    AndyB Well-Known Member

    Thank you, euantor.

    Now I hope someone with more knowledge than I will be able to show me how to code this correctly:

    PHP:
    foreach ($smilietext as $substring)
    {
        
    $smilie_count += substr_count($input['message'], $substring);
    }
     
  19. AndyB

    AndyB Well-Known Member

    Got it!

    PHP:
        $smilie_count 0;  
         foreach (
    $smilietext as $k => $v)
         {
           foreach (
    $v as $k => $substring)
           {
             
    $smilie_count += substr_count($input['message'], $substring);
           }
         }
     
  20. Jeremy

    Jeremy XenForo Moderator Staff Member

    Just use:
    PHP:
    $v['smilie_text']
    The unnecessary for loop is a slight performance hit.

    I'm also 99% sure this query is already in the default models.
     
    euantor likes this.

Share This Page