I have : TEXTRANDOM
I want to replace like:
1T_1E_1X_1T_etc...
Solved:
<?php
$str = "TEXTRANDOM";
$len = strlen($str);
for($i = 0; $i < $len; ++$i)
echo "1".$str[$i]."_";
?>
I have : TEXTRANDOM
I want to replace like:
1T_1E_1X_1T_etc...
Solved:
<?php
$str = "TEXTRANDOM";
$len = strlen($str);
for($i = 0; $i < $len; ++$i)
echo "1".$str[$i]."_";
?>
You can use preg_split('//u', $yourText, -1, PREG_SPLIT_NO_EMPTY)
and array_map
like this:
$yourText = 'TEXTRANDOM';
// execute a function for every letter in your string
$arrayOfTags = array_map(function (string $letter): string {
return '1'.$letter.'_'; // concat every letter with number and _
}, preg_split('//u', $yourText, -1, PREG_SPLIT_NO_EMPTY));
echo implode('', $arrayOfTags); // make the array into a string back again
str_split
will split the string into bytes instead of letters. To split the string into array of letters you can use preg_split('//u', $yourText, -1, PREG_SPLIT_NO_EMPTY)
.
You can also do it with just a loop:
$arrayOfletters = preg_split('//u', $yourText, -1, PREG_SPLIT_NO_EMPTY);
foreach ($arrayOfletters as $letter) {
echo '1'.$letter.'_';
}