dev_marshell08
dev_marshell08

Reputation: 1101

How to insert a character inside a string at a fixed position from the end?

Example: 845, 945, 1045, 1145.

If the length of the string is 3 then I am trying to add a : after the first character, e.g. 8:45. If the length of the string is 4 then I am trying to add : after the second character, e.g. 10:45. Not sure which method would help me do that. Any suggestions?

while($row = mysqli_fetch_array($result))
    {

    $message = $row['Timings'];

    if(strlen($message) = 3)
        {

        } 
    else if(strlen($message) = 4)
        {

        }

Upvotes: 2

Views: 787

Answers (1)

Sharanya Dutta
Sharanya Dutta

Reputation: 4021

Try this:

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

Upvotes: 7

Related Questions