Jump to content

string offset help


dennismonsewicz

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.