Faouzi FJTech
Faouzi FJTech

Reputation: 1071

Preg_replace not working as wanted

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?

Upvotes: 0

Views: 113

Answers (1)

Kirk Backus
Kirk Backus

Reputation: 4886

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)

Upvotes: 2

Related Questions