Insert two points in the string

2

I have the following string :

$valor = "0050";

How do I insert two points after the second house and stay this way?

00:50
    
asked by anonymous 09.09.2017 / 23:18

2 answers

3

If what you have is

$valor = "0050";

Just do it like this:

$valor = "00:50";

If what you have is a variable that has 4 unknown characters and you want to separate them, just do

$valor = substr($valor, 0, 2) . ":" . substr($valor, 2);

If you do not know well the criteria it is difficult to solve, you would have to decide a rule.

Documentation .

    
09.09.2017 / 23:33
0

Another way to resolve this problem is to use the substr_replace () function. What makes it work the way you want it is to report the fourth argument as zero. This will add the character in the position defined in the third argument (in this example to two)

$valor = '0099';
echo substr_replace($valor, ':', 2, 0);
    
13.09.2017 / 14:56