Remove characters from the middle of a string?

Hello everyone, I’m back… already. Hopefully I’m not too much of a pest, and I hope I can someday repay the help I’ve gotten here.

Anyway, I need to remove characters from the middle of a string. I can’t determine in advance how long the string will be, so I can’t count from the end, but I know how far to count in from the beginning. I need to remove characters 40 through 54. I tried using [php]substr($title, 0, 39);[/php] to count in from the right up to what I want to delete and create a new short string from the first part of the long string. Then I was going to use [php]substr($title, 54)[/php] to start collecting characters just after the section I want to delete, creating a second short string from the last portion of the long string. Then I was going to glue the two strings together.

So basically (shortened)…

original string: abcdefg

new string: ab

new string: fg

new long string: abfg

I have a workaround I can use that involves rewriting other parts of the code, but if there’s an easier solution, can anyone provide a hint? With this problem, I feel like I’m a lot closer going into it than the last one I posted here.

Dave

I’m not sure I follow what your question is? Are you saying you have something like this:
[php]
$new_title = substr($title, 0, 39);
$new_title = ($new_title . substr($title, 54));
[/php]

If so what is the problem exactly?

Either use substr to grab sections of the string and then combine them back as Sabmin said.

Or, if the string you want removed is always the same use str_replace…
Such as in your example: $title=str_replace(“cde”, “”, $title); (It will replace all cde’s with blanks…)

Hi Sabmin and ErnieAlex,

I managed to figure out a solution on my own - my problem was that I was miscounting characters and some of the extra code I used for debugging was left in, confusing me. The solution I came up with isn’t as clean as [php]str_replace[/php] can. Then again, if the code to be removed (replaced using [php]str_replace[/php]) is different in every instance, [php]str_replace[/php] wouldn’t work, would it?

If I’m being confusing, it’s only because I’m the one that’s confused. ;D Actually, I do get the concepts, but my brain is getting tired.

Dave

No, do it Sabin’s way. I was explaining string replacements in general.

If you want part of a string, use substr to pull the part you need.
Then, you can combine them back in whatever way you need this data formatted.

If you want to remove part of a string and you just want to erase it AND if you know what the text is,
you can use str_replace.

You can also use various regex and str_preg functions to actually parse the data. These are very nice to remove possible infections and there are many simple one or two line examples on the net to do this. It is very handy when you accept text from a user or guest online. (Protects your database!)

So, that is a recap of general uses for string replacements. Just FYI…

All good stuff! I went with Sabmin’s way and its working fine. I’m using that same function in another spot too. I’ve learned a TON with this project!

Glad to hear it! Looking forward to your next puzzle!

Sponsor our Newsletter | Privacy Policy | Terms of Service