Replacing Spaces with Underscores
$name = str_replace(' ', '_', $name);
PHP str_replace replace spaces with underscores
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);
Replace spaces with a Underscore in a URL
Try using str_replace
for replace space with underscore
<div class="groupinfo">
<a href="<?= base_url() ?>group/group_id/<?= $gr->grp_id ?>/<?= str_replace(" ","_",$gr->grp_name) ?>">
</div>
Replace spaces with underscores in a custom field
Try this
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$field_value = get_post_meta($postid, 'mycustomfield', true);
echo str_replace('_', ' ', $field_value);
wp_reset_query();
?>
uncapitalize all text and replace spaces with underscore in an php array
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));
}
Replace underscore with space and upper case first character in array
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)
Replace an Underscore with a blank space
As I said in the comments, you're doing the str_replace
after you do an echo
so you won't see the changes. If you want to see the changes, you have to do the str_replace
before doing an echo
.
$key="property_type";
echo get_post_meta($post->ID, $key, true ); // get the post meta with the original key
$key = str_replace('_', ' ', $key); // change the key and replace the underscore
echo $key; // will output "property type"
UPDATED ANSWER
I was browsing the WordPress documentation and got an idea of what is happening. Please do this instead:
$key="property_type";
echo str_replace('_', ' ', get_post_meta($post->ID, $key, true )); // get the post meta with the original key but output the result with the value's underscores replaced.
Replacing spaces with underscores with php
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
?>
Related Topics
Apache2 on Ubuntu - PHP Files Downloading
Laravel Polymorphic Relations Has Many Through
PHP Exec() Not Returning Error Message in Output
How to Get the External Ip of My Server Using PHP
Is_Unique for Codeigniter Form Validation
Php: Whats the Total Length of a Post Global Variable
HTML 5 Multi File Upload with PHP
How to Print Multidimensional Arrays in PHP
Move an Array Element to a New Index in PHP
Remove Null, False, and '' - But Not 0 - from a PHP Array
How to Move an Array Element with a Known Key to the End of an Array in PHP
Best Way to Determine If a File Is Empty (Php)
How to Iterate Through Dom Elements in PHP
Which Is the Best Character Encoding for Japanese Language for Db, PHP, and HTML Display
How to Remove Null Values from an Array
Does Static Method in PHP Have Any Difference with Non-Static Method
MySQL Binary Storage Using Blob VS Os File System: Large Files, Large Quantities, Large Problems