Pancreatico Pancreatico - 1 month ago 7
PHP Question

put a coma between two characters

I have this string:

"09001200"
and I need to turn it on this another string:
"09:00 - 12:00"
.

Is there any function in php that put a specific character between two characters? like a ":" between that 90.

Answer

It can be done by sustr_replace method

$newString = substr_replace($oldString, $char_to_insert, $pos, 0);

Example:

echo substr_replace("09001200", ":", 2, 0);

Output: 09:001200

Complete code:

$old = "09001200";
$new = substr_replace($old, ":", 2, 0);
$new = substr_replace($new, " - ", 5, 0);
$new = substr_replace($new, ":", 10, 0);
echo $new;

Output: 09:00 - 12:00

Comments