php 从字符串的开头删除字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4517067/
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:17:01  来源:igfitidea点击:

Remove a string from the beginning of a string

phpstring

提问by Alex

I have a string that looks like this:

我有一个看起来像这样的字符串:

$str = "bla_string_bla_bla_bla";

How can I remove the first bla_; but only if it's found at the beginning of the string?

我怎样才能删除第一个bla_;但前提是它位于字符串的开头?

With str_replace(), it removes allbla_'s.

使用str_replace(),它会删除所有bla_的。

回答by Fabio Mora

Plain form, without regex:

普通形式,没有正则表达式:

$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';

if (substr($str, 0, strlen($prefix)) == $prefix) {
    $str = substr($str, strlen($prefix));
} 

Takes: 0.0369 ms(0.000,036,954 seconds)

耗时:0.0369 毫秒(0.000,036,954 秒)

And with:

与:

$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';
$str = preg_replace('/^' . preg_quote($prefix, '/') . '/', '', $str);

Takes: 0.1749 ms(0.000,174,999 seconds) the 1st run (compiling), and 0.0510 ms(0.000,051,021 seconds) after.

注意到:0.1749毫秒(0.000,174,999秒)第一次运行(编译),和0.0510毫秒(0.000,051,021秒)之后。

Profiled on my server, obviously.

显然,在我的服务器上进行了分析。

回答by Tatu Ulmanen

You can use regular expressions with the caret symbol (^) which anchors the match to the beginning of the string:

您可以使用带有插入符号 ( ^) 的正则表达式,它将匹配锚定到字符串的开头:

$str = preg_replace('/^bla_/', '', $str);

回答by Pavel

function remove_prefix($text, $prefix) {
    if(0 === strpos($text, $prefix))
        $text = substr($text, strlen($prefix)).'';
    return $text;
}

回答by cbrandolino

Here.

这里。

$array = explode("_", $string);
if($array[0] == "bla") array_shift($array);
$string = implode("_", $array);

回答by Aleksandar Bosakov

Nice speed, but this is hard-coded to depend on the needle ending with _. Is there a general version? – toddmo Jun 29 at 23:26

速度不错,但这是硬编码的,取决于以 _ 结尾的针。有通用版吗?– toddmo 6 月 29 日 23:26

A general version:

一般版本:

$parts = explode($start, $full, 2);
if ($parts[0] === '') {
    $end = $parts[1];
} else {
    $fail = true;
}

Some benchmarks:

一些基准:

<?php

$iters = 100000;
$start = "/aaaaaaa/bbbbbbbbbb";
$full = "/aaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee";
$end = '';

$fail = false;

$t0 = microtime(true);
for ($i = 0; $i < $iters; $i++) {
    if (strpos($full, $start) === 0) {
        $end = substr($full, strlen($start));
    } else {
        $fail = true;
    }
}
$t = microtime(true) - $t0;
printf("%16s : %f s\n", "strpos+strlen", $t);

$t0 = microtime(true);
for ($i = 0; $i < $iters; $i++) {
    $parts = explode($start, $full, 2);
    if ($parts[0] === '') {
        $end = $parts[1];
    } else {
        $fail = true;
    }
}
$t = microtime(true) - $t0;
printf("%16s : %f s\n", "explode", $t);

On my quite old home PC:

在我相当旧的家用电脑上:

$ php bench.php

Outputs:

输出:

   strpos+strlen : 0.158388 s
         explode : 0.126772 s

回答by Stefan Gabos

Here's an even faster approach:

这是一个更快的方法:

// strpos is faster than an unnecessary substr() and is built just for that 
if (strpos($str, $prefix) === 0) $str = substr($str, strlen($prefix));

回答by Muzaffar Mahmood

This will remove first match wherever it is found i.e., start or middle or end.

这将删除找到的第一个匹配项,即开始或中间或结束。

$str = substr($str, 0, strpos($str, $prefix)).substr($str, strpos($str, $prefix)+strlen($prefix));

回答by Inca

I think substr_replace does what you want, where you can limit your replace to part of your string: http://nl3.php.net/manual/en/function.substr-replace.php(This will enable you to only look at the beginning of the string.)

我认为 substr_replace 可以满足您的需求,您可以将替换限制为字符串的一部分:http: //nl3.php.net/manual/en/function.substr-replace.php(这将使您只能查看字符串的开头。)

You could use the count parameter of str_replace ( http://nl3.php.net/manual/en/function.str-replace.php), this will allow you to limit the number of replacements, starting from the left, but it will not enforce it to be at the beginning.

您可以使用 str_replace ( http://nl3.php.net/manual/en/function.str-replace.php)的 count 参数,这将允许您限制替换次数,从左侧开始,但它不会强制它在开头。

回答by Bhanu Prakash Pandey

<?php
$str = 'bla_string_bla_bla_bla';
echo preg_replace('/bla_/', '', $str, 1); 
?>

回答by Bog

str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

now does what you want.

现在做你想做的。

$str = "bla_string_bla_bla_bla";
str_replace("bla_","",$str,1);