• 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

AndyB

Well-known member
#1
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.
 

AndyB

Well-known member
#3
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.jpg
 

euantor

Well-known member
#4
Strange. Have you tried casting $substring as a string explicitly:

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

AndyB

Well-known member
#5
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.
 

euantor

Well-known member
#6
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.
 

AndyB

Well-known member
#7
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));
 

AndyB

Well-known member
#8
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();
 

AndyB

Well-known member
#9
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');
   }
}

?>
 

AndyB

Well-known member
#11
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.
 

euantor

Well-known member
#12
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.
 

AndyB

Well-known member
#15
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)
 

AndyB

Well-known member
#18
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);
}
 

AndyB

Well-known member
#19
Got it!

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

Jeremy

Well-known member
#20
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.