将下划线替换为空白 space
Replace an Underscore with a blank space
我正在尝试使用 str_replace 删除下划线并将其替换为空白 space。这被用在一个 word press 模板中,该模板正在提取它正在执行的元键值,但其中仍然有下划线。任何帮助都会很棒,因为我已经尝试了很多东西。我使用的代码如下。
<?php
$key="property_type";
echo get_post_meta($post->ID, $key, true );
$key = str_replace('_', ' ', $key);
?>
顺序应该是:
$key="property_type";
$key = str_replace('_', ' ', $key);
echo get_post_meta($post->ID, $key, true );
正如我在评论中所说,您在执行 echo
之后执行 str_replace
,因此您不会看到更改。如果你想看到变化,你必须做 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"
更新的答案
我正在浏览 WordPress documentation 并了解正在发生的事情。请改为这样做:
$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.
@uom-pgregorio 提供了一个很好的解决方案,谢谢。
<?php$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.?>
我正在尝试使用 str_replace 删除下划线并将其替换为空白 space。这被用在一个 word press 模板中,该模板正在提取它正在执行的元键值,但其中仍然有下划线。任何帮助都会很棒,因为我已经尝试了很多东西。我使用的代码如下。
<?php
$key="property_type";
echo get_post_meta($post->ID, $key, true );
$key = str_replace('_', ' ', $key);
?>
顺序应该是:
$key="property_type";
$key = str_replace('_', ' ', $key);
echo get_post_meta($post->ID, $key, true );
正如我在评论中所说,您在执行 echo
之后执行 str_replace
,因此您不会看到更改。如果你想看到变化,你必须做 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"
更新的答案
我正在浏览 WordPress documentation 并了解正在发生的事情。请改为这样做:
$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.
@uom-pgregorio 提供了一个很好的解决方案,谢谢。
<?php$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.?>