du5407 2013-07-31 17:01
浏览 24
已采纳

Preg_replace无法正常工作

Basically i have the following text stored in $text var :

$text = 'An airplane accelerates down a runway at 3.20 m/s2 for 32.8 s until is finally lifts off the ground. Determine the distance traveled before takeoff'.

I have a function that replaces some keywords on the text from an array named $replacements which is (I did a var_dump on it) :

'm' => string 'meter' (length=5)
'meters' => string 'meter' (length=5)
's' => string 'second' (length=6)
'seconds' => string 'second' (length=6)
'n' => string 'newton' (length=6)
'newtons' => string 'newton' (length=6)
'v' => string 'volt' (length=4)
'speed' => string 'velocity' (length=8)
'\/' => string 'per' (length=3)
's2' => string 'secondsquare' (length=12)

The text goes through the following function :

$toreplace = array_keys($replacements);

foreach ($toreplace as $r){
    $text = preg_replace("/\b$r\b/u", $replacements[$r], $text);
}

However, there is a difference between what I expect and the output :

Expected Output : an airplane accelerates down runway at 3.20 meterpersecondsquare for 32.8 second until finally lifts off ground determine distance traveled before takeoff 

Function Output : an airplane accelerates down runway at 3.20 meterpers2 for 32.8 second until finally lifts off ground determine distance traveled before takeoff 

Notice that I expect 'meterpersecondsquare' and I get 'meterpers2' (the 's2' isn't replaced) while the 'm' and '/' were replaced with their values.

I noticed that when I put m/s instead of m/s2 it works fine and gives :

an airplane accelerates down runway at 3.20 meterpersecond for 32.8 second until finally lifts off ground determine distance traveled before takeoff 

So the problem is basically it doesn't match that s2. Any thoughts why is it the case?

  • 写回答

1条回答 默认 最新

  • dty9731 2013-07-31 17:12
    关注

    Move the s2 replacement before the s replacement.

    Since you are doing the replacement one at a time, you are destroying the s2 before it gets a chance to replace it.

    3.20 m/s2 will be transformed like this

    [m] 3.20 meter/s2

    [s] 3.20 meter/second2

    [/] 3.20 meterpersecond2

    Which results in meterpersecond2

    Here is the proper order

    'm' => string 'meter' (length=5)
    'meters' => string 'meter' (length=5)
    's2' => string 'secondsquare' (length=12)
    's' => string 'second' (length=6)
    'seconds' => string 'second' (length=6)
    'n' => string 'newton' (length=6)
    'newtons' => string 'newton' (length=6)
    'v' => string 'volt' (length=4)
    'speed' => string 'velocity' (length=8)
    '\/' => string 'per' (length=3)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。