I would like to retrieve a part of a string determined by Bytes. For example, if the string is likes this:

myText = "abcdefghijklmn";
I would like to take the first 10 bytes from the string, so the result will be: abcdefghij";

I need to calculate by bytes because I need to conside if the input string may not be English. For example if it is chinese, then one chinese letter may use 2 bytes instead.

    $myText = "abcdefghijklmn";
    echo substr($myText, 0, 10);

      Originally posted by rolwong
      I need to calculate by bytes because I need to conside if the input string may not be English. For example if it is chinese, then one chinese letter may use 2 bytes instead.

      So you wouldn't mind if a Chinese two-byte character got chopped in half at the end?

        Yes, i worry the letter would be chopped.. any method to solve this? thanks

          Originally posted by rolwong
          Yes, i worry the letter would be chopped.. any method to solve this? thanks

          Okay, so you do want characters, not bytes. In that case tekky's suggestion is a good'un.

            Write a Reply...