0

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?

7
  • 1
    Move the s2 replacement before the s replacement. Commented Jul 31, 2013 at 17:07
  • This works however makes the 's' alone fail Commented Jul 31, 2013 at 17:13
  • That's a little odd. What is the exact output? It sounds as if your last element in the replacements array is not being executed. Commented Jul 31, 2013 at 17:15
  • for m/s2 it is working, however for m/s it gives meterpers Commented Jul 31, 2013 at 17:19
  • @FaouziNikolaic Shouldn't $text be using double quotes, instead of single quotes, or does it not matter in this case? Such as $text="string"; Just saying. Commented Jul 31, 2013 at 17:32

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

actually is mentioned on the commment, if moved the s2 works well and gives meterpersecondsquare, however for m/s it doesn't work anymore and gives meterpers, i tried changing the replacements var from s2->secondsquare to second2-> secondsquare following the final output, but not working too...
@FaouziNikolaic, I updated the correct order. The reason your other change didn't work is because the string would be transformed from m/s to meters/s to meterspers which disqualifies s since it is no longer alone if left at the end.
Thanks a lot that ordered made everything work properly ! (moral of the question order is always important)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.