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 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 25 words does not guarantee there will be 170 characters. strpos is returning false; therefore, $offset is not an acceptable argument for substr(). 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? 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; Link to comment https://forums.phpfreaks.com/topic/271205-notice-undefined-offset/#findComment-1395259 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.