7
I have the following string: "AAAAABBBBBCCCCC"
There is a script that puts a dot "." every 5 characters?
In case this script would return: "AAAAA.BBBBB.CCCCC."
7
I have the following string: "AAAAABBBBBCCCCC"
There is a script that puts a dot "." every 5 characters?
In case this script would return: "AAAAA.BBBBB.CCCCC."
10
A solution is the function chunk_split()
which serves precisely to divide the string in smaller pieces:
$str = chunk_split("AAAAABBBBBCCCCCDDDDD", 5, '.');
Example in Ideone:
var_dump($str); // AAAAA.BBBBB.CCCCC.DDDDD.
Note: I’m suggesting this function because in your question you asked not only to add the .
each X positions, but for the result end with .
. With this function, you meet both requirements.
5
You can do it this way:
<?php
$string = "AAAAABBBBBCCCCC";
$array = str_split($string, 5);
$novaString = implode(".", $array);
echo $novaString;
?>
Explaining what happens: You have separated the string into several Strings, and stored it within an array called $array. The result is:
$array[0] has AAAAAAA values
$array[1] has BBBBB values
$array[2] has CCCCC values
And after the separation in the arrays, the implode is used to concatenate the strings stored in the array with the dot (.). I hope it helped..
4
Another way to add a string based on an interval(5 characters) is to use the function wordwrap. The third parameter is the character to be inserted and the fourth forces php to add the character in the exact range, because the purpose of the function is to make the addition between words and how your string is one thing only the fourth parameter is mandatory.
$str = "AAAAABBBBBCCCCCDDDDD";
$intervalo = 5;
$str_formatada = wordwrap($str, $intervalo, '.', true);
echo $str_formatada;
Exit:
AAAAA.BBBBB.CCCCC.DDDDD
Example of the fourth parameter:
1 - Omitted
$str = 'A very long woooooooooooord';
$str_formatada = wordwrap($str, 3, "<br>");
echo $str_formatada;
Exit:
A
very
long
woooooooooooord
As the words were smaller or larger than the specified range(3) the function ended up adding <br>
among them. See how it works when the fourth parameter is marked as true, now the string is broken to exactly 3 characters even if it cuts or ruins the word.
2- With the fourth parameter
Exit:
A
ver
y
lon
g
woo
ooo
ooo
ooo
ord
4
Since they raised the hare on multibyte, I’ll leave an alternative with such support:
$string = 'aaaaabbbbbcccccdddddeeeee1111122222maçãsefeijões';
$new = implode(
'.',
preg_split(
'/(.{5})/us',
$string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
)
);
This produces:
string 'aaaaa.bbbbb.ccccc.ddddd.eeeee.11111.22222.maçãs.efeij.ões' (length=60)
What would be "raised the hare" @Brunoaugusto?
+1 by multibyte support
What is the origin and meaning of the phrase «raised the hare»? (cc @Wallacemaxters)
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
The problem is that
str_split
does not accept multibyte, but if the string does not contain accents, this response is more direct.– Papa Charlie
To link @Papacharlie, I already needed a solution of this and it served me once for precisely not having accents, as he did not specify. Your answer is also very good by dealing with this detail.
– João Neto
But oq is multibyte?
– Nickolas Carlos
@Nickolascarlos, many languages can handle a character in only 1 byte, but there are others that cannot, in the case of Portuguese there are characters that are accented, for example: ÁÃÓÒ among several others, Storing these types of characters requires more than just 1 byte. So characters that need more than 1 byte to be stored are called multibyte.
– João Neto