Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set a character betwen other characters in php

Tags:

php

numbers

clock

how to convert string sample 1 to sample 2 in PHP:

this string : 0510

after : 05:10

thanks

like image 420
erfan Avatar asked Jan 27 '26 05:01

erfan


1 Answers

Without giving more info, there are hundreds of ways to convert '0510' to '05:10'. You can use .substr():

$string = '0510';
$string = substr($string, 0, 2).':'.substr($string, -2);

Or brackets:

$string = $string[0].$string[1].':'.$string[2].$string[3];

Or .str_split() with .implode():

$string = implode(':', str_split($string, 2));

Or .preg_replace():

$string = preg_replace(`~(\d{2})(\d{2})~`, '$1:$2', $string);
like image 69
Tatu Ulmanen Avatar answered Jan 28 '26 19:01

Tatu Ulmanen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!