dongming4994 2018-01-30 19:15
浏览 71
已采纳

Preg_replace问题是剥离数字/字符长度?

Consider the following example....

The issue is with the preg_replace which is used to replace the variables total_balance_overriden and total_balance.

$text = 'this: {{total_balance_overridden}} - that: {{total_balance}}';

$total = '$517.50';
$overiddentotal = '$390.00';

$text = preg_replace('/{{total_balance_overridden}}/', $overiddentotal, $text);
$text = preg_replace('/{{total_balance}}/', $total, $text);


    echo $total;

    echo $overiddentotal;

    echo $text;

This gives me...

$517.50

$390.00

this: 0.00 - that: 7.50

It appears that the $total and $overiddentotal vars have the correct output, but when they have been replaced using the preg_replace, their length has been stripped, and the currency sign and first two numbers are missing. Any ideas why?

Note: If i replace the dollar sign with a pound sign it works! I get...

this: £390.00 - that: £517.50

So is the dollar sign and 2 numbers some sort of special character or var thats getting stripped?

  • 写回答

1条回答 默认 最新

  • douweihui0178 2018-01-30 19:28
    关注

    Dollar signs are special characters in replacement strings, they normally refer to captured substrings from the match. If you want a literal dollar sign, you have to escape it:

    $total = '\$517.50';
    $overiddentotal = '\$390.00';
    

    Note also, in this case, there's no need for regex at all. Just use str_replace() and you won't have this issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂