PHP Replaces Spaces with Underscores

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



Leave a reply



Submit