$str = "ASDFGH";
echo substr_replace($str, "", 1, 1);
In PHP how do I remove a character from a given position in a string [closed]
Вопрос
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
Решение
Другие советы
$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.
Не связан с StackOverflow