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

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

How to remove the first character of string in PHP?

phpstring

提问by yuli chika

$str=':this is a applepie :) ';

How to use PHP, Remove the first character :

PHP如何使用,去掉第一个字符:

回答by mario

The substr()function will probably help you here:

substr()功能可能会在这里帮助您:

 $str = substr($str, 1);

Strings are indexed starting from 0, and this functions second parameter takes the cutstart. So make that 1, and the first char is gone.

字符串从 0 开始索引,此函数的第二个参数采用 cutstart。所以设为 1,第一个字符就消失了。

回答by Haim Evgi

To remove every :from the beginning of a string, you can use ltrim:

要从:字符串的开头删除每个,您可以使用ltrim

$str = '::f:o:';
$str = ltrim($str, ':');
var_dump($str); //=> 'f:o:'

回答by alexn

Use substr:

使用substr

$str = substr($str, 1); // this is a applepie :)

回答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.39602184295654sec

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.153294801712sec

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.2393000125885sec

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.8543920516968sec

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

See PHP manual example 3

参见PHP 手册示例 3

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 substrfixed 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);