| Viewed 84 times

How to get the first 20 characters of the non-English string in PHP


How to get the first 20 characters of the non-English string in PHP

PHP
1 Answer
Mohammad Qandeel
Answered
1

Using mb_strlen() function to get the string length 

after that check, if the length greater than 20 or less 

if string greater than 20 we use mb_substr() function to slice the first 20 characters 

if string less than 20 print the string

Example 

<?php
    $str = "أبجد هوز حط كلمن سعفص قرشت";
    echo (mb_strlen($str,'utf8')>20?mb_substr($str,0,20):$str);
?>ا