0

I have string in PHP "S*+%2YKΠ3)3Π+)+Θ" that I want to iterate.

my code :

$string = "S*+%2YKΠ3)3Π+)+Θ";
$charLength = mb_strlen($string);
for($i = 0 ; $i < $charLength ; $i++) {
    $char = mb_substr($string, $i , 1);
    if($char == 'Π') {
       echo "this is my Π character";
    }
}

But I never entering the echo statement to get "this is my Π character",

Anyone to help me with this problem, please ... I already looking for others answer like:

How to convert any character encoding to UTF8 on PHP

Convert utf8-characters to iso-88591 and back in PHP

PHP: Convert any string to UTF-8 without knowing the original character set, or at least try

How to convert a string to utf-8 code in php

but never work for my case. Thank you

Community
  • 1
  • 1
muyassar
  • 41
  • 1
  • 5

1 Answers1

0

The code as such is fine, however there are a number of possible pitfalls:

  1. You need to tell the mb functions what encoding they're supposed to work on. Either pass the encoding to each one individually (e.g. mb_strlen($string, 'UTF-8')), or set the encoding once for all using mb_internal_encoding.
  2. Your .php source code file must be saved in UTF-8 if you want your string literals to be UTF-8 encoded. Check in your text editor of choice that you're actually saving the file encoded as UTF-8 (no BOM).
deceze
  • 510,633
  • 85
  • 743
  • 889