Problem with character decoding

Asked

Viewed 153 times

0

I’m using a function to limit the amount of characters that are displayed, something like this:

 function blabla ($string, $maxSize) {
    if (strlen($string) > $maxSize)
                $string = substr($string, 0, $maxSize) . ' [...]';
            return $string;
}

Only that it is returning some buged characters like: "title" is "title", but when I do not use the function the string comes normal, no buged characters. how can I fix this?

I’m using this in the header:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  • Where’s that coming from $string?

  • It’s the function input, I’ll edit to clarify.

1 answer

0


  • 1

    Please at all times look for if there is no answer to the problem and then favor the existing answers, in case this question has answer here http://answall.com/q/8235/3635 then you should signal the question as duplicate. We are not a support forum, we are a Q&A and we seek to aggregate content, but if something is very similar really is not recommend duplicating answers. I recommend that when you have time read Help: http://answall.com/help

  • 1

    Thanks for the information, @Guilherme-birth, but I answered the context, and the context of the other question is different. In this the user don’t know that the problem is the function substr(), in the one you mention, he knows. Further: the guidance of the community guide to duplicate questions is for those who ask here. By the way, the team has already said that some duplicates are feasible see here But I’ll take that remark into account. Hug!

  • Just so we’re clear: the problem has nothing to do with the answer given, substr does not convert characters. You probably have more problems with the original code than that. If it is corrupting characters in the middle of the string, it is encoding problem. As much as the mb_ molt in substr is the right or wrong count of characters: http://ideone.com/bDFlhE - and that’s why I closed unilaterally as a duplicate of the one that has to do with the right subject, which is how to deal with encoding. Otherwise I would have closed the other substr and mb_substr existing (which is similar to this answer here).

  • 1

    In fact the Shutupmagda solution worked perfectly, the encoding is utf8 in both html and database.

  • 1

    Yeah, @ngthm4r3, and your question IS NOT DUPLICATED, see? First because it wasn’t encoding problem, but character counting problem, then because the way it was written can help someone who has problems using substr() but who doesn’t know that.

  • 1

    @NGTHM4R3 was not about that what I said, I said that this answer http://answall.com/a/8240/3635 answers your question, after all explains about utf8 and the use of functions with the prefix mb_ ;)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.