Php Warning: Strpos():

I posted this issue in bug tracker but it's not getting much attention and nobody else seems to have the issue so I thought I would pick some brains in the forum.

[07-Apr-2018 14:41:40 America/Chicago] PHP Warning:  strpos(): Offset not contained in string in /home/user/public_html/app/functions/fn.common.php on line 2145

I have been getting these warning for some time now and I have been able to trace it down to only certain product pages but I cannot figure out exactly what is causing it. Can anyone tell me what this function truncates?

function fn_truncate_chars($text, $limit, $ellipsis = '...')
{
    if (strlen($text) > $limit) {
        $pos_end = strpos(str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $text), ' ', $limit);
    if($pos_end !== false)
        $text = trim(substr($text, 0, $pos_end)) . $ellipsis;
}

return $text;

}

Hello,

Could you try matching the timestamp from your warning with the one in the access log to see what dispatch has triggered this warning?

Kind regards,

Like I stated, the warnings occur on various product pages in the frontend.

You'll probably have to add some debugging and look for when the strpos returns false and error_get_last() and look at the "message" element. It should contain 'strpos'. When it does, you can do a debug_backtrace() to see how you got there.

The actual error is that $limit -1 is more than the length of the string. Ie. strpos("a", "abc", 3) would return that error since "abc"[3] does is beyond the length of "abc". So you'll have to see what is calling the function with an invalid $limit.

Okay, apparently this has something to do with the pinterest button from the social buttons addon when truncating the description.


PHP Warning
Message

strpos(): Offset not contained in string
Error at

app/functions/fn.common.php, line: 2145
Backtrace
File: app/functions/fn.common.php
Line: 2145
Function: {closure}
File: app/addons/social_buttons/func.php
Line: 61
Function: fn_truncate_chars
File: app/addons/social_buttons/func.php
Line: 32
Function: fn_pinterest_prepare_settings
File: app/addons/social_buttons/controllers/frontend/products.post.php
Line: 32
Function: fn_get_sb_provider_settings
File: app/functions/fn.control.php
Line: 702
Function: include
File: app/functions/fn.control.php
Line: 460
Function: fn_run_controller
File: index.php
Line: 25
Function: fn_dispatch


    $pinterest_params = array(
‘url’ => urlencode(fn_sb_get_url()),
‘media’ => urlencode(fn_get_sb_image_url($params)),
‘description’ => rawurlencode(fn_truncate_chars(htmlspecialchars(fn_get_sb_description($params)), 200)),
‘params’ => ‘’,
);
This is the description for the product.

EZ Red XLM500-GR EZR-XLM500-GR Xtreme Magnetic Light, Green



  • Includes 360 degree magnetic base

  • Logo COB LED

  • Micro USB recharcheable rotating stand and handle

  • Rugged bumper protectors

  • On/Off Button

Suggest you look at line 61 on your site of the social_buttons/func.php file. On my site it has:

'description' => rawurlencode(fn_truncate_chars(htmlspecialchars(fn_get_sb_description($params)), 200)),

The 200 parameter is supposed to be a max limit. However, there's a bug in that function where the comparison is done BEFORE the str_replace. And the string length is ending up less than $limit because 2 chars can be replaced by 1 "\r\n' replaced with ' '. This alters the length of the string.

Suggest you submit a report to bugtracker with this info.

You can probably fix it by changing

. if (strlen($text) > $limit) {
        $pos_end = strpos(str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $text), ' ', $limit);
   if($pos_end !== false)
        $text = trim(substr($text, 0, $pos_end)) . $ellipsis;
}

Suggest you look at line 61 on your site of the social_buttons/func.php file.


I posted that piece/line of code above and you have what I have.

Suggest you submit a report to bugtracker with this info.


I did a month ago and it's been under review for as long. That's why I am picking your brain. Do you reckon we'll get compensated for fixing it? LMAO

What exactly does ". " do? It's the only thing I see different in your code and the original.

Looks like a chunk of my posting got dropped. There should have been a

to

    $stripped = str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $text),;
    if (strlen($stripped) > $limit) {
        $pos_end = strpos($stripped, $limit);
   if($pos_end !== false)
        $text = trim(substr($text, 0, $pos_end)) . $ellipsis;
}

UNTESTED

Note that in the above, it's possible for the returned text to be less than 200 chars given that any "\r\n" chars (2) will be evaluated against the 200 as a single char..

It looks like it fixed it. You left a comma after $text). The working function is:

function fn_truncate_chars($text, $limit, $ellipsis = '...')
{
    $stripped = str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $text);
    if (strlen($stripped) > $limit) {
        $pos_end = strpos($stripped, $limit);
    if($pos_end !== false)
        $text = trim(substr($text, 0, $pos_end)) . $ellipsis;
}

return $text;

}

Just a friendly note to others...if you use cpanel, do not use the file editor to edit the /apps/functions/fn.common.php. It will not only change the charset within the code in the file but it will save it as gb18030 even if you select utf-8.

Glad it works for you. As I said, UNTESTED.