$name = str_replace(' ', '_', $name);
Answer from Tim Fountain on Stack OverflowJava2Blog
java2blog.com › home › php › replace space with underscore in php
Replace Space with Underscore in PHP [2 ways] - Java2Blog
December 5, 2022 - Use the str_replace() function to replace with underscore in PHP, for example: str_replace(" " , "_", $str). str_replaces() returns new String with old character space( ) replaced by underscore(_)
IQCode
iqcode.com › code › php › php-replace-space-with-underscore
php replace space with underscore Code Example
October 7, 2021 - $str = str_replace(' ', '_', $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 ·
SourceTrail
sourcetrail.com › home › php › solved: replace space with underscore
Solved: replace space with underscore in PHP - SourceTrail
September 19, 2023 - Ultimately, we use the echo statement to display the new string that has the spaces replaced with underscores. In the end, instead of displaying ‘Hello World’, the code prints ‘Hello_World’, thus effectively replacing the space with underscores. As part of additional information, it’s important to know that you can also use libraries in PHP to perform string manipulation operations.
Techareatutorials
techareatutorials.com › how-to-replace-space-with-underscore-in-php
How to Replace Space with Underscore in PHP - techareatutorials.com
By the using of str_replace() function we can replace white space from string. ... <?php $str = "Hello Welcome to Tech Area"; $str = str_replace(' ','_',$str); //Replace Space with Underscore echo $str; ?>
DaniWeb
daniweb.com › programming › web-development › threads › 109751 › replace-a-space-with-an-underscore-solved
php - Replace a space with an underscore SOLVED | DaniWeb
February 19, 2008 - Use simple string functions for trivial replacements; reach for regex only when you need pattern matching (as @nav33n noted). If the goal is a URL-friendly "slug" (more than just replacing spaces), normalize the text: transliterate to ASCII, replace non-alphanumerics with underscores, collapse repeats, trim, lowercase and limit length. Example slug function:
Top answer 1 of 3
7
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
?>
2 of 3
1
Allow me to introduce you to str_replace()
$var = str_replace(' ', '_', $var);
Plugin-planet
plugin-planet.com › usp-pro-replace-underscores-spaces
USP Pro – Replace Underscores with Spaces | Plugin Planet
July 4, 2021 - // usp pro replace underscores with spaces function usp_pro_modify_select_value($value) { return strtolower(trim(str_replace('_', ' ', $value))); } add_filter('usp_custom_fields_select_value', 'usp_pro_modify_select_value'); Add that to your theme’s functions.php file and done.
Savecode
savecode.net › code › php › php+replace+space+with+underscore
php replace space with underscore - SaveCode.net
July 13, 2020 - $str = str_replace(' ', '_', $str);
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.
Laracasts
laracasts.com › discuss › channels › general-discussion › convert-space-to-underscore
Convert <space> to <underscore>
The most popular Laravel and PHP programming forum.
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 )