In PHP how do I remove a character from a given position in a string [closed]

StackOverflow https://stackoverflow.com/questions/20417982

  •  29-08-2022
  •  | 
  •  

I would like to always delete the character in the 2nd position within a string. The trouble is, this could literally be anything.

Sample inputs would be:

5H23098
SHISL
S-SLSYNSL
M/SHGIKT

Output should be:

523098
SISL
SSLSYNSL
MSHGIKT
有帮助吗?

解决方案

$str = "ASDFGH";
echo substr_replace($str, "", 1, 1);

其他提示

$s1 = $str[0];
$s2 = substr($str, 2);

$result = $s1.$s2;

$s = substr($s,0,1).substr($s,2);

Also it's possible to do:

$str = 'abcd';
$str[1] = '';

But take into the account that

strlen($str) == 4
$string = "5H23098";

$newString = $string[0].substr($string, 2);

Result: 523098

Takes the first character + all characters after position 2.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top