Shifty
Shifty

Reputation: 112

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?

Upvotes: 0

Views: 641

Answers (3)

Shifty
Shifty

Reputation: 112

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!

Upvotes: 0

Shifty
Shifty

Reputation: 112

Now I tried with this code:

function charfunction($myStr,$limit=17) {    
    $result = "";
    for($i=0; $i<$limit; $i++) {
        $result .= $myStr[$i];
    }
    return $result;    
}

And write out:

echo charfunction($row['title'])." ...";

The full title in $row['title'] is A végzet ereklyéi - Csontváros

If I use $limit=17 it writes A végzet erekly� ...

If I change to $limit=18 it writes A végzet ereklyé ...

Counts the � simbol for two characters. But the é character is in that position it appears �. Why?

Upvotes: 0

Skamielina
Skamielina

Reputation: 802

Use iconv function to change charset from latin2 to utf-8 and then make mb_substr.

echo iconv("ISO-8859-2","UTF-8//TRANSLIT", $string);

Upvotes: 1

Related Questions