drgaeqdqiiyg14608 2015-11-21 09:16
浏览 81
已采纳

PHP - 如何使用trim可选的character_mask去除我想要剥离的所有字符

I need some help understanding the trim function. From the docs, its says the following about character_mask:

character_mask Optionally, the stripped characters can also be specified using the character_mask parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.

When I test by specifying trim($num,"$\,"), its not working as I expected.

$num = '$1,000.00';

$findSymbol = strpos($num, '$'); // find $ symbol
if ($findSymbol === false) {
    $num = $num;
}else{
    $num = trim($num,"$\,"); // strip the $ and the , symbol
}

var_dump($num); //OUTPUT: 1,000.00

Basically, all I want is strip the $ and the , symbols all together so the output would look like this:

1000.00

Any help is greatly appreciated. Thanks!

  • 写回答

5条回答 默认 最新

  • dtfbj24048 2015-11-21 09:18
    关注

    trim() strips the listed characters from the beginning and/or end of the string as described in the docs you've quoted.

    Strip whitespace (or other characters) from the beginning and end of a string

    If you want to remove characters from the middle of a string, use str_replace()

    $num = str_replace(['$', ','], '', $num); 
    

    You can also use the two functions in combination:

    $num = str_replace(',', '', trim($num, '$')); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?