0

I want to change the string "T Shirts Vests" into "TShirts Vests". I have tried this:

$item['menu_3'] = str_ireplace("T Shirts Vests", "TShirts", $item['menu_3']);

But str_ireplace doesn't seem to be able to handle that. How should I do this?

2
  • 2
    What is the output you are getting? And why are you using str_ireplace rather than str_replace? Do you need the case insensitivity? Commented Feb 8, 2012 at 22:18
  • 4
    The code you've typed makes perfect sense and works without error, except that you're replacing the entire string "T Shirts Vests" with "TShirts". We can't help you without knowing the content of $item['menu_3']). Please edit your question to include it. Commented Feb 8, 2012 at 22:18

2 Answers 2

6
$item['menu_3'] = str_ireplace("T Shirts Vests","TShirts Vests", $item['menu_3']);

str_replace will work fine too by the way.

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry it was a typo, I dont want vests. Never the less this dosent work :( But If I replace just one word, eg. Shirts, it works
I'm not sure .. str_replace really just does that. It could fail if a character is not what you expect it to be. Like, there could be an invisible character in the string.
Checked the database and cant see any hidden characters :/
That's the point of hidden characters :P Loop over each character and print the character code.
Again. Until you tell us what the contents of $item['menu_3'] are, we cannot help you. Please var_dump($item['menu_3']) and update your question to include the output.
|
0
$str = "T Shirts Vests";
$str[strpos($str, " ")] = '';

2 Comments

This will replace every space, so T Shirts Vests becomes TShirtsVests, which I don't think is what he's after.
thx @Sam, I initially thought the last parameter to str_replace (count) told PHP to stop at a certain number of replacements. But in fact, this is a variable you hand into the function that gets set. Fixed.

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.