1

I am trying to iterate through a string that contains french characters. I have an array of specific french characters that I am looking for that I want to match.

Example:

header("Content-Type: text/html;charset=utf-8");
$string = "HÂPPY Ç" ;
echo $string;//displays correctly
echo "<br>";
$frenchArray = Array('Â','Ç');
for($i=0;$i<mb_strlen($string);$i++)
{
    $t = utf8_encode(mb_substr($string,$i,1));
    echo "Checking:" . $t . "<br>";
    for($x=0;$x<count( $frenchArray);$x++)
    {
        if($t==$frenchArray[$x])
        {
            echo "Matched: " . $t . " to " . $frenchArray[$x]."<br>";
        }
    }
}

Which gives me no matches:

Checking:H
Checking:Ã
Checking:
Checking:P
Checking:P
Checking:Y
Checking:
Checking:Ã
Checking:

I notice that the characters are not displayed correctly in the "Checking:"

Seems like there is something simple that I'm missing here, any help is appreciated!

user3473534
  • 131
  • 1
  • 10
  • 1
    You might find [this note on the PHP mb_substr documentation](http://php.net/manual/en/function.mb-substr.php#117764) useful. Essentially, looping in this way using mb_substr is rather slow. I would also imagine that your issue is related to `utf8_encode`; i.e. your original string is already UTF-8. – Luke Briggs Dec 13 '16 at 17:12

1 Answers1

1

Seems like I missed the ",'UTF-8'" part of the "mb_". Thank you for linking to the article Luke.

The correct code should be:

for($i=0;$i<mb_strlen($string,'UTF-8');$i++)
{
    $t = mb_substr($string,$i,1,'UTF-8');
    echo "Checking:" . $t. "<br>";
    for($x=0;$x<count( $frenchArray);$x++)
    {
        if(preg_match('/'.$t.'/',$frenchArray[$x]))
        {
            echo "Matched: " . $t . " to " . $frenchArray[$x]."<br>";
        }
    }
}
user3473534
  • 131
  • 1
  • 10