开发者

php simple function remove from characters of a word

开发者 https://www.devze.com 2023-01-13 16:07 出处:网络
I need a simple function(e.g function_cut) to cut from a variable characters if the number of characters is bigger than 14.

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

精彩评论

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