Custom Meta Field - Remove comma from string with str_replace

I have a website that display carvehicle listings. Each listing displays the PRICE. The price is set as a custom field

I have a website that display car/vehicle listings. Each listing displays the PRICE. The price is set as a custom field on a custom post type, that is set to a string data type. The user typically types the price in as like 8,999. I need to echo/output/display the data with the comma removed, like this: 8999

Currently I display the data like this:

<?php echo get_post_meta($post->ID, "_price", true); ?>

How can I use str_replace to output this data without the comma? Or is there a simpler, better way? Any suggestions would be appreciated.

I have a website that display car/vehicle listings. Each listing displays the PRICE. The price is set as a custom field on a custom post type, that is set to a string data type. The user typically types the price in as like 8,999. I need to echo/output/display the data with the comma removed, like this: 8999

Currently I display the data like this:

<?php echo get_post_meta($post->ID, "_price", true); ?>

How can I use str_replace to output this data without the comma? Or is there a simpler, better way? Any suggestions would be appreciated.

Share Improve this question asked Aug 15, 2019 at 0:35 PrestonPreston 114 bronze badges 2
  • 2 $price = get_post_meta( $post->ID, '_price', true ); echo str_replace( ',', '', $price ); would do it. – Sally CJ Commented Aug 15, 2019 at 5:29
  • 1 @SallyCJ I literally just figured this out on my own. Very similar to your answer. Thank your help. Ill post my answer as well. – Preston Commented Aug 15, 2019 at 6:05
Add a comment  | 

1 Answer 1

Reset to default 0

Literally just figured this out on my own by reviewing str_replace documentation again.

<?php
  $price_meta = get_post_meta($post->ID, "_price", true);
  $price_meta_stripped = str_replace(',', '', $price_meta);
  echo $price_meta_stripped;
?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745240391a4618147.html

相关推荐

  • Custom Meta Field - Remove comma from string with str_replace

    I have a website that display carvehicle listings. Each listing displays the PRICE. The price is set as a custom field

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信