4

I am trying to use some specific symbols in a string. I need to loop through each character of the string and identify the symbols. However, certain symbols are getting converted to THREE CHARACTERS. Need help on how to retain the symbol.

echo $instring = ("& ∨ = ⊢");
echo "\nLength of string: ".strlen($instring); 
for ($i = 0; $i < strlen($instring) ; $i++){
    $temp_str = substr($instring, $i,1);
    echo "\nChar: $i: $temp_str";
    $instring_arr[$i] = $temp_str;
}

HERE IS THE WORKING CODE. Thanks to h2oooooo:

echo $instring = ("& ∨ = ⊢");
echo "\nLength of string: ".mb_strlen($instring, "UTF-8"); 
for ($i = 0; $i < mb_strlen($instring, "UTF-8") ; $i++){
    $temp_str = mb_substr($instring, $i,1,"UTF-8");
    echo "\nChar: $i: $temp_str";
    $instring_arr[$i] = $temp_str;
}
  • 4
    That's because `substr` and `strlen` works on an ascii basis (every character is considered 1 byte - `0-255` or `00000000-11111111` in binary), and these characters are unicode (each character can be up to [*6* bytes](http://en.wikipedia.org/wiki/UTF-8)). Use the [multibyte functions](http://dk1.php.net/manual/en/ref.mbstring.php) [`mb_substr`](http://www.php.net/mb_substr) and [`mb_strlen`](http://www.php.net/mb_strlen) instead. – h2ooooooo May 26 '14 at 13:34
  • Hi h2ooooooo, You made my day! I don't know how to "accept" / vote for your answer. I will post the working code. THanx a ton. – Pranav Thakkar May 26 '14 at 18:55

1 Answers1

0

I think you can juste split the string, put all the parts on Table (or List or ever else) and check all the Table till you find your specific symbols.

Hope I help you.

  • Thanks but I don't thunk it'll work. PHP is treatin. Certain symbols, like the one that looks like v, as three characters. The length of the string is also being reported as a higher value. – Pranav Thakkar May 26 '14 at 13:22