Sowmya
Sowmya

Reputation: 43

strlen function in php

I have some items in database as below

Item       Description
--------------------------
Item 1     Some text here
Item 2     Some text
Item 3     Some text here
Item 4     Some 
Item 5     Some text here

Here the client requirement is if the description is more that 15 characters then we should display only 15 characters and should display some dotted line (. . . . ) . In case if the description is less than 15 characters then we will display the full text (now dotted line should not be there)

For that I have written the below code.

<?php                                                           
    if(strlen($row['description'])>15) {
        echo mb_substr($row['description'],0,15,"UTF-8");
        echo" . . . . .";
    }
    else
    {
        echo $row['description'];
    }
?>

In case of english language that code is working fine but in case of Japanese language it is creating problem. Means even the text is less than 15 chars dotted line is getting displayed (Only in few cases)

What may be the problem?

Upvotes: 2

Views: 948

Answers (3)

pardeep
pardeep

Reputation: 21

It would help you...........

<?php 

if(mb_strlen($row['description'])>15) {

    echo mb_substr($row['description'],0,15,"UTF-8");

    echo" . . . . .";

}

else

{

    echo $row['description'];

}

?>

Upvotes: 0

Kristian Vitozev
Kristian Vitozev

Reputation: 5971

When you dealing with multibyte characters, you should use mb_xxx functions, here is the implementation of strlen: mb_strlen

Edit: You can read more about multibyte string functions: http://www.php.net/manual/en/ref.mbstring.php

Upvotes: 6

silkfire
silkfire

Reputation: 25945

Use mb_strlen(), as it will return the correct length when working with multibyte charsets.

Upvotes: 3

Related Questions