how to convert string sample 1 to sample 2 in PHP:
this string : 0510
after : 05:10
thanks
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With