$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);
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.
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.
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);
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.
Savecode
savecode.net › code › php › php+replace+space+with+underscore
php replace space with underscore - SaveCode.net
July 13, 2020 - $str = str_replace(' ', '_', $str);
Laracasts
laracasts.com › discuss › channels › general-discussion › convert-space-to-underscore
Convert <space> to <underscore>
The most popular Laravel and PHP programming forum.
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
Replace space with underscore character in string - PHP Coding Help - PHP Freaks
November 24, 2006 - I have a variable containing a string submitted from a form:[code]$string = ($_POST["string"]);[/code]I'm struggling to build some code that would replace any spaces in the string with the underscore character and then store back into a different variable $string2...Could anyone gi...