php 只用 str_replace 替换字符串的第一个字符?

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

Replace only the first character of a string with str_replace?

phpstringstr-replace

提问by Vinny

I'm trying to replace only the first character of a string, but it isn't working, it replaces all the "0" on the string, here's my code:

我试图只替换字符串的第一个字符,但它不起作用,它替换了字符串上的所有“0”,这是我的代码:

$mes2 = str_replace('0', '', $mes[0]);

I want to replace the first character only if its a 0, example:

我只想替换第一个字符,如果它是 0,例如:

07 becomes 7

07变成7

I don't want to replace all the time, example:

我不想一直更换,例如:

11 becomes 1, i don't want it.

11 变成 1,我不要。

I also tried this way, but it didn't work the way i want, because it's replacing also the second character if it's 0, like 10 becomes 1.

我也试过这种方式,但它没有按我想要的方式工作,因为如果它是 0,它也会替换第二个字符,比如 10 变成 1。

$mes2 = preg_replace('/0/', '', $mes, 1);

回答by Doug McLean

OK, based on refinements to your question, what you probably want is ltrim.

好的,根据对您的问题的改进,您可能想要的是ltrim.

$out = ltrim($in, "0");

This will strip all leading zeroes from $in. It won't remove zeroes from anywhere else, and it won't remove anything other than zeroes. Be careful; if you give it "000" you'll get back "" instead of "0".

这将从$in. 它不会从其他任何地方删除零,也不会删除除零以外的任何内容。当心; 如果你给它“000”,你会得到“”而不是“0”。

You could use typecasting instead, as long as $inis always a number (or you want it to result in 0 if it isn't):

您可以改用类型转换,只要$in它始终是一个数字(或者如果不是,您希望它产生 0):

$out = (int) $in;
  • 007 becomes 7
  • 000 becomes 0
  • 100 stays as 100
  • 456 stays as 456
  • 00a becomes 0
  • 56a becomes 0
  • ab4 becomes 0
  • -007 becomes -7
  • 007变成7
  • 000 变成 0
  • 100 保持为 100
  • 456 保持为 456
  • 00a 变为 0
  • 56a 变为 0
  • ab4 变为 0
  • -007 变成 -7

...etc.

...等等。

Now, in the unlikely event that you only want to replace the first 0, so for example "007" becomes "07", then your latest attempt mentioned in your question is almost there. You just need to add a "caret" character to make sure it only matches the start of the string:

现在,万一您只想替换第一个 0,例如“007”变为“07”,那么您在问题中提到的最新尝试几乎就在那里。您只需要添加一个“插入符号”字符以确保它只匹配字符串的开头:

$out = preg_replace('/^0/', '', $in);

回答by cpugourou

$str = '1ere is the solution';
echo preg_replace('/1/', 'h', $str, 1); // outputs 'here is the solution'

回答by Doug McLean

Use substr:

使用substr

$mes2 = substr($mes, 1);

This will removethe first character, which is what it looks like you're trying to achieve. If you want to replace it with something, use this:

这将删除第一个字符,这就是您想要实现的样子。如果您想用某些东西替换它,请使用以下命令:

$mes2 = $new_char . substr($mes, 1);

Edit

编辑

I may have misread your question - if $mes[0]is the original string, use $mes[0]in place of $mesabove.

我可能误读了您的问题-如果$mes[0]是原始字符串,请使用$mes[0]代替$mes上面的字符串。

回答by Azenis

Strings in PHP can be accessed just like array

PHP 中的字符串可以像数组一样访问

<?php
$var = "test";
$var[0] = "1";
var_dump($var);
?>

Will output string(4) "1est"

会输出 string(4) "1est"

回答by Pello

This is how I would have done it:

这就是我会这样做的方式:

$mes2 = substr_replace($mes[0],'',strpos($mes[0], '0'),1);

回答by mandza

You could do that like this:

你可以这样做:

$mes2 = "07 test subject";
if($mes2['0']=='0') { $mes2['0']=''; }
echo $mes2;

This will output:

这将输出:

7 test subject

回答by Standej

My long approach:

我的长期做法:

$first_letter = substr($mes[0], 0, 1);
$rest = substr($mes[0], 1);
$mes2 = str_replace('0', '', $first_letter);
$result = $mes2.$rest;

Or shorter:

或更短:

$result = str_replace('0', '', substr($mes[0], 0, 1)).substr($mes[0], 1);

回答by max

You may simple use $mes[0] = '';or if you need specific substitution something like $mes[0] = $mes[0] == '<replaced character>' ? '' : $mes[0];

您可以简单使用,$mes[0] = '';或者如果您需要特定的替换,例如$mes[0] = $mes[0] == '<replaced character>' ? '' : $mes[0];