dennismonsewicz Posted April 29, 2010 Share Posted April 29, 2010 I have the following function: function string_into_parts($text, $length=45) { $text = strval(trim($text)); $array = array(); $counter = 0; if(strlen($text) > $length) { for($i=0;$text[$length+$i]!=" ";$i++) { //line 67 if(!$text[$length+$i]) { //line 68 return $text; } } $array['firstHalf'] = trim(substr($text,0,$length+$i)); $array['secondHalf'] = trim(substr($text,$length+$i)); } else { $array['firstHalf'] = $text; $array['secondHalf'] = ''; } return $array; } but if you run it on a string that contains an individual number, IE: it broke into 2 pieces, the script fails and I receive the Uninitialized string offset error message. The script fails at lines 67 & 68. Anyone know why this could be failing? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/200185-string-offset-help/ Share on other sites More sharing options...
teamatomic Posted April 29, 2010 Share Posted April 29, 2010 Nothings wrong with the code. Just seems like a lot of lines to do just this: $length=45; $str='this is a test for the string length split it should work just fine with a lot less code'; $max=strlen("$str"); $sub1=substr("$str", 0, "$length"); $sub2= substr("$str", "$length","$max" ); echo "$sub1::$sub2"; HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/200185-string-offset-help/#findComment-1050607 Share on other sites More sharing options...
dennismonsewicz Posted April 29, 2010 Author Share Posted April 29, 2010 This works perfect!!!!!! Thank you SOOO MUCH! Link to comment https://forums.phpfreaks.com/topic/200185-string-offset-help/#findComment-1050642 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.