Fritex Fritex - 1 year ago 54
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:


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

Wanted result:


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 Source

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

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