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