php 如何在PHP中删除字符串的第一个字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4638569/
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
How to remove the first character of string in PHP?
提问by yuli chika
回答by mario
回答by Haim Evgi
回答by Hayenn
Exec time for the 3 answers :
3个答案的执行时间:
Remove the first letter by replacing the case
通过更换大小写删除第一个字母
$str = "hello";
$str[0] = "";
// $str[0] = false;
// $str[0] = null;
// replaced by ?, but ok for echo
Exec time for 1.000.000 tests : 0.39602184295654
sec
1.000.000 次测试的执行时间:0.39602184295654
秒
Remove the first letter with substr()
用 substr() 删除第一个字母
$str = "hello";
$str = substr($str, 1);
Exec time for 1.000.000 tests : 5.153294801712
sec
1.000.000 次测试的执行时间:5.153294801712
秒
Remove the first letter with ltrim()
用 ltrim() 删除第一个字母
$str = "hello";
$str= ltrim ($str,'h');
Exec time for 1.000.000 tests : 5.2393000125885
sec
1.000.000 次测试的执行时间:5.2393000125885
秒
Remove the first letter with preg_replace()
使用 preg_replace() 删除第一个字母
$str = "hello";
$str = preg_replace('/^./', '', $str);
Exec time for 1.000.000 tests : 6.8543920516968
sec
1.000.000 次测试的执行时间:6.8543920516968
秒
回答by Tarun modi
Here is the code
这是代码
$str = substr($str, 1);
echo $str;
Output:
输出:
this is a applepie :)
回答by Chandan Sharma
To remove first Character of string in PHP,
要在 PHP 中删除字符串的第一个字符,
$string = "abcdef";
$new_string = substr($string, 1);
echo $new_string;
Generates: "bcdef"
回答by Gammerz
$str = substr($str, 1);
echo substr('abcdef', 1); // bcdef
Note:
笔记:
unset($str[0])
will not workas you cannot unset part of a string:-
将不起作用,因为您无法取消设置字符串的一部分:-
Fatal error: Cannot unset string offsets
回答by rybo111
Update
更新
After further tests, I don't recommend using this any more. It caused a problem for me when using the updated string in a MySQL query, and changing to substr
fixed the problem. I thought about deleting this answer, but comments suggest it is quicker somehow so someone might have a use for it. You may find trimming the updated string resolves string length issues.
经过进一步的测试,我不建议再使用它。在 MySQL 查询中使用更新的字符串时,它给我带来了一个问题,并更改为substr
修复该问题。我想删除这个答案,但评论表明它以某种方式更快,所以有人可能会使用它。您可能会发现修剪更新的字符串可以解决字符串长度问题。
Sometimes you don't need a function:
有时你不需要一个函数:
$str[0] = '';
For example:
例如:
$str = 'AHello';
$str[0] = '';
echo $str; // 'Hello'
This method modifies the existing string rather than creating another.
此方法修改现有字符串而不是创建另一个字符串。
回答by Dan Bray
The accepted answer:
接受的答案:
$str = ltrim($str, ':');
works but will remove multiple :
when there are more than one at the start.
有效,但当:
开始时有多个时会删除多个。
$str = substr($str, 1);
will remove any character from the start.
将从头开始删除任何字符。
However,
然而,
if ($str[0] === ':')
$str = substr($str, 1);
works perfectly.
完美地工作。
回答by Adeojo Emmanuel IMM
you can use the sbstr() function
您可以使用 sbstr() 函数
$amount = 1; //where $amount the the amount of string you want to delete starting from index 0
$str = substr($str, $amount);