FUNKAM35 Posted November 26, 2012 Share Posted November 26, 2012 Hi, I am getting a : Undefined offset with the following code $words=str_word_count($desc); if($words >25) { $offset = strpos ($desc, " " ,170); $desc = substr ($desc,0,$offset); $desc.= "......"; } I only want to display a certain length of the desc which works but I get this error message, any ideas welcome thanks Notice Quote Link to comment https://forums.phpfreaks.com/topic/271205-notice-undefined-offset/ Share on other sites More sharing options...
mrMarcus Posted November 26, 2012 Share Posted November 26, 2012 (edited) 25 words does not guarantee there will be 170 characters. strpos is returning false; therefore, $offset is not an acceptable argument for substr(). Edited November 26, 2012 by mrMarcus Quote Link to comment https://forums.phpfreaks.com/topic/271205-notice-undefined-offset/#findComment-1395253 Share on other sites More sharing options...
FUNKAM35 Posted November 26, 2012 Author Share Posted November 26, 2012 thanksmrmarcus, so how do I fix this? Quote Link to comment https://forums.phpfreaks.com/topic/271205-notice-undefined-offset/#findComment-1395258 Share on other sites More sharing options...
mrMarcus Posted November 26, 2012 Share Posted November 26, 2012 If 170 is the limit before truncation, then skip the words check and just do a character count: $count = strlen($desc); $max = 170; if ($count > $max) { $desc = substr($desc, 0, $max); $desc .= '......'; } echo $desc; Quote Link to comment https://forums.phpfreaks.com/topic/271205-notice-undefined-offset/#findComment-1395259 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.