如何从字符串中删除前导字符?

发布于 2024-10-10 18:08:02 字数 156 浏览 6 评论 0原文

我有一个输入字符串,例如:

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

如何使用 PHP 删除第一个出现的 :

期望的输出:这是一个苹果派:)

I have a input string like:

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

How can I remove the first occurring : with PHP?

Desired output: this is a applepie :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

不即不离 2024-10-17 18:08:02

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

 $str = substr($str, 1);

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

The substr() function will probably help you here:

 $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.

池木 2024-10-17 18:08:02

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

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

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

$str = '::f:o:';
$str = ltrim($str, ':');
var_dump($str); //=> 'f:o:'
夕嗳→ 2024-10-17 18:08:02

使用 substr

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

Use substr:

$str = substr($str, 1); // this is a applepie :)
诗笺 2024-10-17 18:08:02

3 个答案的执行时间:

通过替换大小写来删除第一个字母

$str = "hello";
$str[0] = "";
// $str[0] = false;
// $str[0] = null;
// replaced by �, but ok for echo

1.000.000 次测试的执行时间:0.39602184295654


用 substr() 删除第一个字母

$str = "hello";
$str = substr($str, 1);

1.000.000 次测试的执行时间:5.153294801712


使用 ltrim() 删除第一个字母

$str = "hello";
$str= ltrim ($str,'h');

1.000.000 次测试的执行时间:5.2393000125885


使用 preg_replace() 删除第一个字母

$str = "hello";
$str = preg_replace('/^./', '', $str);

1.000.000 次测试的执行时间:6.8543920516968

Exec time for the 3 answers :

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


Remove the first letter with substr()

$str = "hello";
$str = substr($str, 1);

Exec time for 1.000.000 tests : 5.153294801712 sec


Remove the first letter with ltrim()

$str = "hello";
$str= ltrim ($str,'h');

Exec time for 1.000.000 tests : 5.2393000125885 sec


Remove the first letter with preg_replace()

$str = "hello";
$str = preg_replace('/^./', '', $str);

Exec time for 1.000.000 tests : 6.8543920516968 sec

标点 2024-10-17 18:08:02

接受的答案:

$str = ltrim($str, ':');

有效,但当开始时有多个 : 时,会删除多个 :

$str = substr($str, 1);

将从头开始删除任何字符。

然而,

if ($str[0] === ':')
    $str = substr($str, 1);

工作完美。

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.

烂柯人 2024-10-17 18:08:02

更新

经过进一步测试,我不建议再使用它。当我在 MySQL 查询中使用更新后的字符串时,它给我带来了一个问题,并且更改为 substr 解决了这个问题。我想过删除这个答案,但评论表明它以某种方式更快,所以有人可能会使用它。您可能会发现修剪更新的字符串可以解决字符串长度问题。


有时您不需要函数:

$str[0] = '';

例如:

$str = 'AHello';
$str[0] = '';
echo $str; // 'Hello'

此方法修改现有字符串而不是创建另一个字符串。

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.


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.

青衫负雪 2024-10-17 18:08:02
$str = substr($str, 1);

请参阅 PHP 手册示例 3

echo substr('abcdef', 1);     // bcdef

注意:

unset($str[0]) 

不会工作< /em> 因为您无法取消设置字符串的一部分:-

Fatal error: Cannot unset string offsets
$str = substr($str, 1);

See PHP manual example 3

echo substr('abcdef', 1);     // bcdef

Note:

unset($str[0]) 

will not work as you cannot unset part of a string:-

Fatal error: Cannot unset string offsets
旧夏天 2024-10-17 18:08:02

使用 mb_substr 函数

    mb_substr("我abc", 1);

use mb_substr function

    mb_substr("我abc", 1);
李白 2024-10-17 18:08:02

按照正常的trim(),从字符串的开头和结尾+空格以及可选的额外单个字符中修剪数组中每个单词的出现次数

<?php
function trim_words($what, $words, $char_list = '') {
    if(!is_array($words)) return false;
    $char_list .= " \t\n\r\0\x0B"; // default trim chars
    $pattern = "(".implode("|", array_map('preg_quote', $words)).")\b";
    $str = trim(preg_replace('~'.$pattern.'$~i', '', preg_replace('~^'.$pattern.'~i', '', trim($what, $char_list))), $char_list);
    return $str;
}

// for example:
$trim_list = array('AND', 'OR');

$what = ' OR x = 1 AND b = 2 AND ';
print_r(trim_words($what, $trim_list)); // => "x = 1 AND b = 2"

$what = ' ORDER BY x DESC, b ASC, ';
print_r(trim_words($what, $trim_list, ',')); // => "ORDER BY x DESC, b ASC"
?>

Trims occurrences of every word in an array from the beginning and end of a string + whitespace and optionally extra single characters as per normal trim()

<?php
function trim_words($what, $words, $char_list = '') {
    if(!is_array($words)) return false;
    $char_list .= " \t\n\r\0\x0B"; // default trim chars
    $pattern = "(".implode("|", array_map('preg_quote', $words)).")\b";
    $str = trim(preg_replace('~'.$pattern.'$~i', '', preg_replace('~^'.$pattern.'~i', '', trim($what, $char_list))), $char_list);
    return $str;
}

// for example:
$trim_list = array('AND', 'OR');

$what = ' OR x = 1 AND b = 2 AND ';
print_r(trim_words($what, $trim_list)); // => "x = 1 AND b = 2"

$what = ' ORDER BY x DESC, b ASC, ';
print_r(trim_words($what, $trim_list, ',')); // => "ORDER BY x DESC, b ASC"
?>
静谧幽蓝 2024-10-17 18:08:02

该代码对我来说效果很好。

$str = substr($str ,-(strlen($str)-1));

也许,也贡献一下答案。

The code works well for me.

$str = substr($str ,-(strlen($str)-1));

Maybe, contribute with answers too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文