1

I'm having an annoying PHP notice error appear since upgrading to PHP 5.4. I can see where its happening but I just don't have the expertise to correct the code and stop the notice.

PHP Notice: Uninitialized string offset: 0

Its happening on line 108 of the code which actually refers to this line: $contents[$o] = $this->unicode_entity_replace($contents[$o]);

The function in question is as follows:

class unicode_replace_entities {
        public function UTF8entities($content="") {
            $contents = $this->unicode_string_to_array($content);
            $swap = "";
            $iCount = count($contents);
            for ($o=0;$o<$iCount;$o++) {
                $contents[$o] = $this->unicode_entity_replace($contents[$o]);
               $swap .= $contents[$o];
            }
            return mb_convert_encoding($swap,"UTF-8");
        }

Any assistance or explanation would be much appreciated.

EDIT: Full code for remainder of the class below (apologies it wasn't included before):

            public function unicode_string_to_array( $string ) { //adjwilli
            $strlen = mb_strlen($string);
            $array = "";
            while ($strlen) {
                $array[] = mb_substr( $string, 0, 1, "UTF-8" );
                $string = mb_substr( $string, 1, $strlen, "UTF-8" );
                $strlen = mb_strlen( $string );
            }
            return $array;
        }

        public function unicode_entity_replace($c) { //m. perez
            $h = ord($c{0});   
            if ($h <= 0x7F) {
                return $c;
            } else if ($h < 0xC2) {
                return $c;
            }

            if ($h <= 0xDF) {
                $h = ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
                $h = "&#" . $h . ";";
                return $h;
            } else if ($h <= 0xEF) {
                $h = ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6 | (ord($c{2}) & 0x3F);
                $h = "&#" . $h . ";";
                return $h;
            } else if ($h <= 0xF4) {
                $h = ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12 | (ord($c{2}) & 0x3F) << 6 | (ord($c{3}) & 0x3F);
                $h = "&#" . $h . ";";
               return $h;
            }
        }
3
  • Don't guess or make us guess, var_dump($contents, $o) to see what you're trying to work with here. Commented Jul 29, 2015 at 12:37
  • please provide unicode_string_to_array code Commented Jul 29, 2015 at 13:10
  • Hi thanks for the comments, full code now above. Commented Jul 29, 2015 at 13:41

3 Answers 3

1

If you use array syntax on a string such as $s[1] it will access the second letter. However, if the string is empty you will get an offset error. You could prevent this using !empty($s) or make sure you are really using an array with is_array().

Sign up to request clarification or add additional context in comments.

2 Comments

he does not access $conetnt string as an array in given code
you don't know about his unicode_string_to_array() function. so you cannot really say. however, I gave a solution for another case too.
0

How about foreach?

class unicode_replace_entities {
        public function UTF8entities($content="") {
            $contents = $this->unicode_string_to_array($content);
            $swap = "";
            $iCount = count($contents);
            foreach ($contents as $k=>$v) {
                $contents[$k] = $this->unicode_entity_replace($v);
               $swap .= $contents[$k];
            }
            return mb_convert_encoding($swap,"UTF-8");
        }

1 Comment

Tried this and now get 'PHP Warning: Invalid argument supplied for foreach()'
0

I've managed to get this figured out. As pointed out by another helpful soul it was $array = ""; in unicode_string_to_array function is causing $array to be a string, not an array. It should be initialized using $array = array();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.