0

I have a string 'some value' and I am looking for the function that replaces certain values with another one.

For example I need the

'some value'

to become

'some_value'

This is what I have tried so far with no luck

  $newCategory=str_replace(' ', '_', $rows["category"]); 
  echo $newCategory;
6
  • Yes its working fine , may be you are doing some mistake. ideone.com/65V2lA Commented Dec 18, 2013 at 5:07
  • wha'st wrong with above code ? Commented Dec 18, 2013 at 5:07
  • it doesn't want to output 'some_value' for some reason with no errors Commented Dec 18, 2013 at 5:08
  • i'm an idiot... i had an extra echo before $newCategory=str_replace(' ', '_', $rows["category"]); Commented Dec 18, 2013 at 5:09
  • 1
    Best of luck and always go through at least one time on your code for some minor issues.sometimes it happens. Commented Dec 18, 2013 at 5:16

2 Answers 2

3

This works fine for me

$rows["category"] =  'some value';
$newCategory=str_replace(' ', '_', $rows["category"]); 
echo $newCategory;

Output

some_value

Display errors could be turned off in the php.ini or your apache config file.

You can turn it on in the script and see any error is there

 error_reporting(E_ALL);
 ini_set('display_errors', 1);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the response. I had an extra echo before my code... :( I will accept your answer in 5 mins.
Welcome @Geo. Enjoy coding.:-)
1

Your code works for me. Still you can give this a try:

$newCategory = preg_replace('/\s+/', '_', $rows["category"]);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.