$name = str_replace(' ', '_', $name);
Answer from Tim Fountain on Stack OverflowI'll suggest that you use this as it will check for both single and multiple occurrence of white space (as suggested by Lucas Green).
$journalName = preg_replace('/\s+/', '_', $journalName);
instead of:
$journalName = str_replace(' ', '_', $journalName);
Try this instead:
$journalName = preg_replace('/\s+/', '_', $journalName);
Explanation: you are most likely seeing whitespace, not just plain spaces (there is a difference).
use the str_replace() function:
<?php
$old = 'OMG_This_Is_A_One_Stupid_Error';
$new = str_replace(' ', '_', $old);
echo $old; // will output OMG This Is A One Stupid error
?>
Reverse the parameters to obtain the reverse effect
<?php
$old = 'OMG This Is A One Stupid_Error';
$new = str_replace('_', ' ', $old);
echo $old; // will output OMG_This_Is_A_One_Stupid error
?>
Allow me to introduce you to str_replace()
$var = str_replace(' ', '_', $var);
You can use the array_map function.
function modify($str) {
return ucwords(str_replace("_", " ", $str));
}
Then in just use the above function as follows:
$states=array_map("modify", $old_states)
Need to use array_map function like as
$state = array("gujarat","andhra_pradesh","madhya_pradesh","uttar_pradesh");
$state = array_map(upper, $state);
function upper($state){
return str_replace('_', ' ', ucwords($state));
}
print_r($state);// output Array ( [0] => Gujarat [1] => Andhra pradesh [2] => Madhya pradesh [3] => Uttar pradesh )
How about?
$before = '<product name_here>Product A</product_name here>';
$after = preg_replace('/(<[^>]*)\s+([^<]*>)/', '$1_$2', $before);
echo $after;
This should give
<product_name_here>Product A</product_name_here>
The parts before and after \s+ specify that you don't want the spaces outside of a tag pairing but just the ones who are enclosed between an opening tag < and a closing tag >.
The $1 and $2 substitute back in the strings before and after the replaced whitespace.
Can you give a better example?
In my understanding you want this:
You have: Product A
You want: Product A
Is that correct?
If that's the case all you need to do is str_replace(' ','_',$product)
Sorry for my bad english.