$name = str_replace(' ', '_', $name);
Answer from Tim Fountain on Stack OverflowIQCode
iqcode.com › code › php › php-replace-space-with-underscore
php replace space with underscore Code Example
October 7, 2021 - $str = str_replace(' ', '_', $str);
Techareatutorials
techareatutorials.com › how-to-replace-space-with-underscore-in-php
How to Replace Space with Underscore in PHP - techareatutorials.com
<?php $str = "Hello Welcome to Tech Area"; $str = str_replace(' ','_',$str); //Replace Space with Underscore echo $str; ?>
Top answer 1 of 4
209
I'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);
2 of 4
23
Try this instead:
$journalName = preg_replace('/\s+/', '_', $journalName);
Explanation: you are most likely seeing whitespace, not just plain spaces (there is a difference).
Grabthiscode
grabthiscode.com › php › php-replace-space-with-underscore
Php replace space with underscore - code example - GrabThisCode.com
php replace spaces with dash · php convert words with spaces to camelcase · php string underscore into camelcase · php insert hyphen into spaces in string · php convert spaces to underscores · php replace \n by <br> remove empty spaces php · php split string along spaces ·
Stack Exchange
wordpress.stackexchange.com › questions › 239580 › replace-underscore-on-space
php - Replace Underscore (_) on Space ( ) - WordPress Development Stack Exchange
September 17, 2016 - Now the question: How to remove Underscore (_) in post title Tell me "where to dig"? I know that there are such a function "str_replace". Where apply it? ... $postData = array( 'post_title' => $attachment->post_title, 'post_type' => 'post', 'post_content' => $image_tag, 'post_category' => array('0'), 'post_status' => 'publish' ); P.S. I not quite understand php
Savecode
savecode.net › code › php › php+replace+space+with+underscore
php replace space with underscore - SaveCode.net
July 13, 2020 - $str = str_replace(' ', '_', $str);
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
PHP - Replace space in file name's with underscore.. - PHP Coding Help - PHP Freaks
July 11, 2009 - Hello, I have a file uploader.... I want to automatically replace any file that has been uploaded that has a space with an underscore (_) at anytime. Example: If Jerry uploaded a file called jerry file.txt, I want to be able to automatically replace it with jerry_file.txt... Any possible way of d...
Mefancy
mefancy.com › textchange › replace-space-underscore
Replace Spaces with Underscores (and more) - Online Text Tool | MeFancy
Programming Variables: Most programming languages (Python, C, PHP) do not allow spaces in variable names. user id becomes user_id (snake_case). Data Cleaning: Standardizing data formats for database imports often requires replacing spaces with specific delimiters. Enter Text: Paste your text into the input box or drag and drop a file. Select Mode: Choose whether you want to replace spaces with underscores, hyphens, or vice versa.
Top answer 1 of 2
2
I recommend using array_walk_recursive
$array = array('this_that','is','an','array');
array_walk_recursive($array,function(&$v){
$v = str_replace('_',' ',$v);
});
print_r($array);
2 of 2
0
Try this:
$array = array('this_that','is','an','array true');
function fixArrayKey(&$arr)
{
$arr=array_combine(array_keys($arr),array_map(function($str){return str_replace(Array('true','_'),Array('YES',' '),$str);},array_values($arr)));
foreach($arr as $key=>$val)
{
if(is_array($val)) fixArrayKey($arr[$key]);
}
return $arr;
}
fixArrayKey($array);
print_r($array);
echo implode("\n",$array);
Top answer 1 of 4
42
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)
2 of 4
4
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 )
Top answer 1 of 3
8
foreach ($all_regions as $key => $value){
$all_regions[$key] = strtolower(str_replace(' ', '_', $value));
}
php.net - str_replace()
Edit
Even better would be the following (I think), because it will be faster because of the internal value pointer. (I will benchmark this)
foreach ($all_regions as &$value){
$value = strtolower(str_replace(' ', '_', $value));
}
2 of 3
2
foreach($all_regions as $key => $val) {
$all_regions[$key] = strtolower(str_replace(' ', '_', $val));
}