Примеры преобразования строк текста в массив по разным разделителям.
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Proin blandit magna eu tempus ullamcorper.
Sed porta justo sed nibh elementum condimentum.
Quisque non eros sit amet elit commodo maximus eget a eros.";
$array = explode("\n", $text);
print_r($array);
Результат:
Array
(
[0] => Lorem ipsum dolor sit amet, consectetur adipiscing elit.
[1] => Proin blandit magna eu tempus ullamcorper.
[2] => Sed porta justo sed nibh elementum condimentum.
[3] => Quisque non eros sit amet elit commodo maximus eget a eros.
)
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin blandit
magna eu tempus ullamcorper! Sed porta justo sed nibh elementum condimentum.
Quisque non eros sit amet elit commodo maximus eget a eros?";
$text = str_replace("\n", '', $text);
$array = preg_split('/(?<=[.?!])\s+(?=[a-zа-яё])/i', $text);
print_r($array);
Результат:
Array
(
[0] => Lorem ipsum dolor sit amet, consectetur adipiscing elit.
[1] => Proin blandit magna eu tempus ullamcorper!
[2] => Sed porta justo sed nibh elementum condimentum.
[3] => Quisque non eros sit amet elit commodo maximus eget a eros?
)
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin blandit magna eu tempus ullamcorper.";
$text = preg_replace("/[^a-zа-яё0-9\s]/i", '', $text);
$array = preg_split('/(\s)/', $text);
$array = array_diff($array, array(''));
print_r($array);
Результат:
Array
(
[0] => Lorem
[1] => ipsum
[2] => dolor
[3] => sit
[4] => amet
[5] => consectetur
[6] => adipiscing
[7] => elit
[8] => Proin
[9] => blandit
[10] => magna
[11] => eu
[12] => tempus
[13] => ullamcorper
)
Результат:
Array
(
[0] => L
[1] => o
[2] => r
[3] => e
[4] => m
[5] =>
[6] => i
[7] => p
[8] => s
[9] => u
[10] => m
[11] =>
[12] => d
[13] => o
[14] => l
[15] => o
[16] => r
[17] =>
[18] => s
[19] => i
[20] => t
[21] =>
[22] => a
[23] => m
[24] => e
[25] => t
)
Разделители -
и :
$text = "Lorem ipsum dolor sit amet-proin blandit magna eu:Sed porta justo.";
$array = preg_split('/[-|:]/u', $text, -1, PREG_SPLIT_NO_EMPTY);
print_r($array);
Результат:
Array
(
[0] => Lorem ipsum dolor sit amet
[1] => proin blandit magna eu
[2] => Sed porta justo.
)
Если разделитель из нескольких символов, например <br>
и </br>
:
$text = "Lorem ipsum dolor sit amet,<br>proin blandit magna eu.</br>Sed porta justo.";
$array = preg_split('/(<br>)|(<\/br>)/u', $text, -1, PREG_SPLIT_NO_EMPTY);
print_r($array);
Результат:
Array
(
[0] => Lorem ipsum dolor sit amet,
[1] => proin blandit magna eu.
[2] => Sed porta justo.
)
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin blandit magna eu tempus ullamcorper.";
$chunks = 10;
$array = str_split($text);
$chunks = array_chunk($array, $chunks, false);
$result = array();
foreach ($chunks as $chunk) {
$result[] = implode($chunk);
}
print_r($result);
Результат:
Array
(
[0] => Lorem ipsu
[1] => m dolor si
[2] => t amet, co
[3] => nsectetur
[4] => adipiscing
[5] => elit. Pro
[6] => in blandit
[7] => magna eu
[8] => tempus ull
[9] => amcorper.
)