15

I want to be able to specify an index in a string and remove it.

I have the following:

"Hello World!!"

I want to remove the 4th index (o in Hello). Here would be the end result:

"Hell World!!"

I've tried unset(), but that hasn't worked. I've Googled how to do this and that's what everyone says, but it hasn't worked for me. Maybe I wasn't using it right, idk.

4
  • 3
    Use substr. Get the first few charactes, get the last few characters after your index, then concatenate. Commented Feb 23, 2013 at 3:42
  • I've tried unset(), but that hasn't worked. I've Googled how to do this and that's what everyone says, but it hasn't worked for me. Commented Feb 23, 2013 at 3:43
  • str_split into an array remove the index then implode back to string. Commented Feb 23, 2013 at 3:47
  • In PHP, strings are not treated as arrays, they have their own set of operations. Commented Feb 23, 2013 at 11:36

5 Answers 5

24

This is a generic way to solve it:

$str = "Hello world";
$i = 4;
echo substr_replace($str, '', $i, 1);

Basically, replace the part of the string before from the index onwards with the part of the string that's adjacent.

See also: substr_replace()

Or, simply:

substr($str, 0, $i) . substr($str, $i + 1)
Sign up to request clarification or add additional context in comments.

Comments

12
$str="Hello World";
$str1 = substr($str,0,4);
$str2 = substr($str,5,7);
echo $str1.$str2;

3 Comments

I would probably throw the string into an array and just remove the 4th key.
@AarolamaBluenk strings != arrays in php :)
On line 3: The integer 7 as third argument of substr isn't required to make it work.
2

This php specific of working with strings also bugged me for a while. Of course natural solution is to use string functions or use arrays but this is slower than directly working with string index in my opinion. With the following snippet issue is that in memory string is only replaced with empty � and if you have comparison or something else this is not good option. Maybe in future version we will get built in function to remove string indexes directly who knows.

$string = 'abcdefg';
$string[3] = '';
var_dump($string);
echo $string;

Comments

0
$myVar = "Hello World!!";

$myArray = str_split($myVar);
array_splice($myArray, 4, 1);

$myVar = implode("", $myArray);

Personal I like dealing with arrays.

(Sorry about lack of code brackets putting this up via my phone)

1 Comment

Using arrays is neither shorter nor efficient; besides, string functions aren't that hard are they? :)
0

I think can create a function and call it like this

    function rem_inx ($str, $ind)
    { 
       return substr($str,0,$ind++). substr($str,$ind);
    }

    //use
    echo rem_inx ("Hello World!!", 4);     

1 Comment

return substr($str,0,$ind--). substr($str,$ind); fixes the above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.