Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strlen function in php

Tags:

php

strlen

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?

like image 652
Sowmya Avatar asked Dec 05 '25 07:12

Sowmya


1 Answers

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

like image 152
Kristian Vitozev Avatar answered Dec 07 '25 00:12

Kristian Vitozev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!