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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4286423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:31:52  来源:igfitidea点击:

Remove first 4 characters of a string with PHP

phpstringcharacter

提问by Belgin Fish

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

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

回答by Daniel Vandersluis

You could use the substrfunction 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."

回答by Gumbo

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

如果您使用的是多字节字符编码,不只是要删除的前四个字节喜欢substr做,使用多字节对应mb_substr。这当然也适用于单字节字符串。

回答by Eng Hany Atwa

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

回答by Tarun modi

You could use the substrfunction please check following example,

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

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

Output: nmodi

回答by Sathish Kumar

$num = "+918883967576";

$str = substr($num, 3);

echo $str;

Output:8883967576

输出:8883967576

回答by Rejneesh Raghunath

use php's built in substrfunction...

使用 php 的内置substr函数...

$result = substr($string, $startFromCharacter, $characterCountToRemove);

$result = substr("Hello World", 0, 5); 

//will return World

回答by Obaidul Haque

You can use this by php function with substr function

您可以通过带有 substr 函数的 php 函数使用它

<?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 文本。示例文本。