-2

I have this kind of string: DURATION : 00:23:55.060000000

I want to convert it to this: 00:23:55.060000000

Please note that after DURATION, it has many spaces.

EDIT:

It seems that I made you upset, guys. :D

I did this and not working: preg_replace('/^Duration,\s+/', '', $result[20])

How to do it with php?

Ari
  • 3,637
  • 3
  • 29
  • 42

3 Answers3

2

Your regex is messed up. You are looking for something in uppercase and your regex is in lowercase. And there is a comma laying around.

So if you rewrite that like:

preg_replace('/^Duration\s+: /i', '', $result[20])

(the i modifier after the regular expression says its case insenstive)

or:

preg_replace('/^DURATION\s+: /', '', $result[20])

It'll work.

But mostly, it seems that you want to catch the timestamp, and disregard the rest. For me, the code would be much clearer if your regex reflected that.

E.g.:

if (preg_match("|(?<timestamp>\d\d:\d\d\:\d\d\.\d{9})|", $string, $matches)) {
     echo $matches['timestamp'];
}
yivi
  • 23,845
  • 12
  • 64
  • 89
0

Solution :

$duration = substr($duration, strpos($duration, (":")) + 2);
Jerry
  • 851
  • 11
  • 16
-1

I hope this can be useful for others who need it:

preg_replace('/duration|^(.*?):|\s/i', '', $result[20]);

code explanation: first, strip the duration, and then the first colon : lastly all spaces. put i at the end to the regex to declare that the search is incase-sensitive.

Ari
  • 3,637
  • 3
  • 29
  • 42
  • already posted it as an answer, with commentary and alternative solution. :) – yivi Jan 25 '17 at 09:25
  • are you sure it is working? it is not working on my side. test it like this: `$var = "DURATION : 00:23:55.060000000";` `preg_replace('/^Duration\s+: /i', '', $var);` – Ari Jan 25 '17 at 09:31