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

时间:2023-01-19 20:22:04

How can I remove the first 4 characters of a string using PHP?

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

6 个解决方案

#1


321  

You could use the substr function to return a substring starting from the 5th character:

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

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

#2


22  

If you’re using a multi-byte character encoding and do not just want to remove the first four bytes like substr does, use the multi-byte counterpart mb_substr. This does of course will also work with single-byte strings.

如果您使用的是多字节字符编码,并且不只是想要删除前4个字节,比如substr,那么使用多字节对应的mb_substr。这当然也适用于单字节字符串。

#3


9  

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;
}

example

例子

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

#4


4  

You could use the substr function please check following example,

您可以使用substr函数请检查以下示例,

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

Output: nmodi

#5


0  

You can use this by php function with substr function

可以使用php函数和substr函数

<?php
function removeChar($value) {
    $value2 = substr($value, 4); 
    return $value2;
}

echo removeChar("Dummy Text. Sample Text.");
?>

You get this result: " y Text. Sample Text. "

你会得到这样的结果:“y文本。示例文本。”

#6


0  

$num = "+918883967576";

$str = substr($num, 3);

echo $str;

Output:8883967576

输出:8883967576

#1


321  

You could use the substr function to return a substring starting from the 5th character:

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

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

#2


22  

If you’re using a multi-byte character encoding and do not just want to remove the first four bytes like substr does, use the multi-byte counterpart mb_substr. This does of course will also work with single-byte strings.

如果您使用的是多字节字符编码,并且不只是想要删除前4个字节,比如substr,那么使用多字节对应的mb_substr。这当然也适用于单字节字符串。

#3


9  

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;
}

example

例子

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

#4


4  

You could use the substr function please check following example,

您可以使用substr函数请检查以下示例,

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

Output: nmodi

#5


0  

You can use this by php function with substr function

可以使用php函数和substr函数

<?php
function removeChar($value) {
    $value2 = substr($value, 4); 
    return $value2;
}

echo removeChar("Dummy Text. Sample Text.");
?>

You get this result: " y Text. Sample Text. "

你会得到这样的结果:“y文本。示例文本。”

#6


0  

$num = "+918883967576";

$str = substr($num, 3);

echo $str;

Output:8883967576

输出:8883967576