Search code examples
phpstringsubstrnon-ascii-characters

How can I split a string with accents in PHP?


I would like to split hurgarian strings which have accents. Now I use this code:

if(strlen($row['title'])<=20)
    {
        echo $row['title'];
    }
    else
    {
        echo substr($row['title'], 0, 17)." ...";
    }

I store these datas with latin2_hungarian_ci coding in the database and I use charset in php files. Both in PHP and HTML part:

header('Content-Type: text/html; charset=utf-8');

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

But with this way if the last character is a non english character (é,á,ö,ü,ó,ő,ú,ű,í) it isn't appears well. Against this character appears a � simbol. If I don't use substr just write the whole tite out everíthing works good.

Now for example: A végzet erekly� ... or Északi széless� ...

I can't understand this substr, because in my examples from one of them write 15 characters and that simbol, from the other one 16 characters and that simbol.

How can I write out the first x characters from all of them?


Solution

  • I solve the problem with this way:

    if(strlen($row['title'])<=20)
        {
            echo $row['title'];
        }
        else
        {
            $result = "";
            $alphas = range('A', 'Z');
            $last = substr($row['title'], 15, 16);
            $title = $row['title'];
            if($alphas != "%".$last."%")
            {
                for($i=0; $i<18; $i++) {
                    $result .= $title[$i];
                }
    
            }
            else
            {
                for($i=0; $i<17; $i++) {
                    $result .= $title[$i];
                }
            }
            echo $result." ...";
        }
    

    Thank you everyone the helps!