I need to change a string from a format such as
abcdefghijklmnopqrstuvwxyz
to ab/cd/ef/abcdefghijklmnopqrstuvwxyz
.
What is the most efficient way to do this in php?
Examples:
123456789
would become 12/34/56/123456789
gwn58yh045bgw0r8
would become gw/n5/8y/gwn58yh045bgw0r8
You want preg_replace
. This code should do it:
$input = 'abcdefghijklmnopqrstuvwxyz';
$output = preg_replace('/^(..)(..)(..).*$/', "$1/$2/$3/$input", $input);
This take the first 3 groups of two characters (..)
and prepends them ($1
, $2
, $3
) to the original string, with slashes.