$input = 'asdqwe123jklzxc';
echo str_replace(substr($input, 3, 8), '', $input);
php remove n chars from nth index of a string
Question
This question relates to an existing topic here..
Remove first 4 characters of a string with PHP
but instead what if i would like to remove specific number of characters from a specific index of a string?
e.g
(i want to remove 8 characters from the fourth index)
$input = 'asdqwe123jklzxc';
$output = 'asdlzxc';
Solution 2
OTHER TIPS
I think you need this:
echo substr_replace($input, '', 3, 8);
More information here:
You can try with:
$output = substr($input, 0, 3) . substr($input, 11);
Where 0,3
in first substr
are 4 letters in the beggining and 11
in second is 3+8
.
For better expirience you can wrap it with function:
function removePart($input, $start, $length) {
return substr($input, 0, $start - 1) . substr($input, $start - 1 + $length);
}
$output = removePart($input, 4, 8);
I think you can try :
function substr_remove(&$input, $start, $length) {
$subpart = substr($input, $start, $length);
$input = substr_replace($input, '', $start, $length);
return $subpart;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow