使用PHP删除字符串的前4个字符


Answers:


413

您可以使用该substr函数返回从第5个字符开始的子字符串:

$str = "The quick brown fox jumps over the lazy dog."
$str2 = substr($str, 4); // "quick brown fox jumps over the lazy dog."

24

如果您使用的是多字节字符编码,而又不想像前一样删除前四个字节substr,请使用多字节对应字符mb_substr。当然,这也适用于单字节字符串。


12
function String2Stars($string='',$first=0,$last=0,$rep='*'){
  $begin  = substr($string,0,$first);
  $middle = str_repeat($rep,strlen(substr($string,$first,$last)));
  $end    = substr($string,$last);
  $stars  = $begin.$middle.$end;
  return $stars;
}

$string = 'abcdefghijklmnopqrstuvwxyz';
echo String2Stars($string,5,-5);   // abcde****************vwxyz

1
并不是完全这样的解释,而是一个很好的演示,说明了将substr()用于字符串操作的各个位。
拜森2014年

5

您可以使用该substr功能,请检查以下示例,

$string1 = "tarunmodi";
$first4 = substr($string1, 4);
echo $first4;

Output: nmodi

3
$num = "+918883967576";

$str = substr($num, 3);

echo $str;

输出:8883967576



By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.