Trying to use the PHP substr_count and getting an error

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.
 
Tried var_dumping $substring?

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.webp
 
Strange. Have you tried casting $substring as a string explicitly:

PHP:
foreach ($smilietext as $substring) {
    $smilie_count += substr_count($input['message'], (string) $substring);
}
 
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.
 
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.
 
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));
 
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();
 
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');
   }
}

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

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.
 
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.
 
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)
 
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);
}
 
Got it!

PHP:
    $smilie_count = 0;  
     foreach ($smilietext as $k => $v)
     {
       foreach ($v as $k => $substring)
       {
         $smilie_count += substr_count($input['message'], $substring);
       }
     }
 
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.
 
Top Bottom