Fritex Fritex - 7 months ago 19
PHP Question

PHP regex match numbers from string before specified character

I have string that its output result could be like:

First : 14:15
Second: 14:2
Third: 10:2
Fourht: 1:20
Fifth: 1:5


I have regex that matches only first and third example from above:

/(^[0-9]{2})/


If there is output like 1:10 or 1:2 there is no output (error).

Wanted result:

14
14
10
1
1


Can I do it with preg_replace() or some other? Do I need to change my regex too? If so, which regex should I use to match numbers before ":" character?

Thanks for info and help!

Answer

To re-state, regex is not the correct tool for this job.

$output = explode(":",$input)[0];