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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.