Remove Letter From A String Based On Position

I’m trying to write a function to remove a letter from a string based on the position of the letter in a string.

I know a character in a string can be accessed as such:

$string = "test"
print $string[1] //outputs "e"

Now how do I choose to remove this letter from the string. I’ve tried:

$word = removeLetter("Fancy",2);
print $word; //Should output "Facy"
function removeLetter($word,$pos){
	unset($word[$pos]);
	return $word;
}

However, this throws an error as a string cannot be used with unset. I’ve also tried setting $word[$pos] = null, but this does not work either. Any suggestions?

There’s lots of ways, but most straightforward is probably to use substr() twice(once for the part before and once for the part after), then concatenate.

You could cheat and use substr_replace() though.

Out of most other languages, PHP has the most amazing collection of string functions

For example, using substr you could do it with:


$word = substr($word, 0, $pos) . substr($word, $pos + 1);