开发者

php simple function remove from characters of a word

I need a simple function(e.g function_cut) to cut from a variable characters if the number of characters is bigger than 14. For e开发者_如何转开发xample the current $user has 18 characters and I need to cut 4 of them (doesn't matter which one).

 $user = "abc123abc123abc123";

 $user = function_cut($user);


substr

$first_14_characters = substr( $string, 0, 14 );


substr($user, 0, 14)


<?php
    $user = "abc123abc123abc123";
    $user = substr($user, 0, 14);
?>


All the solutions are fine, but if you have UTF8 data, you should use the multibyte versions of the function, namely mb_substr:

function cut($str){ 
  return mb_substr($str, 0, 14); 
}


You can use the substr function like this:

function cut($str){
  return substr($str, 0, 14);
}

$user = cut($user);
echo $user;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜