8

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:

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

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

07 becomes 7

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

11 becomes 1, i don't want it.

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.

$mes2 = preg_replace('/0/', '', $mes, 1);
Vinny
  • 141
  • 1
  • 1
  • 6

8 Answers8

31

OK, based on refinements to your question, what you probably want is 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".

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

$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

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

$out = preg_replace('/^0/', '', $in);
Doug McLean
  • 1,289
  • 12
  • 26
7

Use substr:

$mes2 = substr($mes, 1);

This will remove the 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 $mes above.

Carl0s1z
  • 4,683
  • 7
  • 32
  • 47
Doug McLean
  • 1,289
  • 12
  • 26
  • 1
    But you don't know if the first character really was a 0 which you wanted to replace. – Rizier123 Nov 25 '15 at 22:10
  • I don't believe that's what the OP wants to achieve. I think the 0 in the question is looking for the character at index 0, not a physical "0". – Doug McLean Nov 25 '15 at 22:11
  • I want to replace only if the first character is a 0, example: 07 become 7, this code would replace all the time, like 11 would become 1, i don't want it. – Vinny Nov 25 '15 at 22:43
  • This is the correct answer to a different question. – mickmackusa Aug 19 '21 at 13:57
4
$str = '1ere is the solution';
echo preg_replace('/1/', 'h', $str, 1); // outputs 'here is the solution'
cpugourou
  • 775
  • 7
  • 11
  • The OP has clarified that they do not want to replace the 2nd character (or later). This solution doesn't meet the brief. – mickmackusa Aug 19 '21 at 13:56
2

Strings in PHP can be accessed just like array

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

Will output string(4) "1est"

Azenis
  • 77
  • 1
0

You could do that like this:

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

This will output:

7 test subject
mandza
  • 330
  • 9
  • 24
-1

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

max
  • 2,757
  • 22
  • 19
-1

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);
Standej
  • 749
  • 1
  • 4
  • 11
-1

This is how I would have done it:

$mes2 = substr_replace($mes[0],'',strpos($mes[0], '0'),1);
skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
Pello
  • 1
  • 1
  • I don't like the look of the loose usage of `strpos()`. This looks like, at best, the correct answer to a different question. – mickmackusa Aug 19 '21 at 13:58