У меня есть строка, которая содержит текст:
$string = "1 In the beginning, God created the heavens and the earth. 2 The earth was without form and void, and darkness was over the face of the deep. And the Spirit of God was hovering over the face of the waters. 3 And God said, “Let there be light,” and there was light. 4 And God saw that the light was good. And God separated the light from the darkness. 5 God called the light Day, and the darkness he called Night. And there was evening and there was morning, the first day. 6 And God said, “Let there be an expanse1 in the midst of the waters, and let it separate the waters from the waters.” 7 And God made2 the expanse and separated the waters that were under the expanse from the waters that were above the expanse. And it was so. 8 And God called the expanse Heaven.3 And there was evening and there was morning, the second day. 9 And God said, “Let the waters under the heavens be gathered together into one place, and let the dry land appear.” And it was so. 10 God called the dry land Earth,4 and the waters that were gathered together he called Seas. And God saw that it was good.";
То, что я хочу, это \ r \ n (новая строка) для каждого числа / стиха.
$regex = '/\d*+\s+(?=[0-9])/'; $string = preg_replace($regex, '<br>', $string); //for HTML output $string = preg_replace($regex, '\r\n', $string); //for txt file
Это сохранит ведущее число каждой строки. Вывод:
1 In the beginning, God created the heavens and the earth. 2 The earth was without form and void, and darkness was over the face of the deep. And the Spirit of God was hovering over the face of the waters. 3 And God said, “Let there be light,” and there was light. 4 And God saw that the light was good. And God separated the light from the darkness. 5 God called the light Day, and the darkness he called Night. And there was evening and there was morning, the first day. 6 And God said, “Let there be an expanse1 in the midst of the waters, and let it separate the waters from the waters.” 7 And God made2 the expanse and separated the waters that were under the expanse from the waters that were above the expanse. And it was so. 8 And God called the expanse Heaven.3 And there was evening and there was morning, the second day. 9 And God said, “Let the waters under the heavens be gathered together into one place, and let the dry land appear.” And it was so. 10 God called the dry land Earth,4 and the waters that were gathered together he called Seas. And God saw that it was good.
Вы можете использовать preg_replace () и 'br'. Ниже код может вам помочь.
$string = "1 In the beginning, God created the heavens and the earth. 2 The earth was without form and void, and darkness was over the face of the deep. And the Spirit of God was hovering over the face of the waters. 3 And God said, “Let there be light,” and there was light. 4 And God saw that the light was good. And God separated the light from the darkness. 5 God called the light Day, and the darkness he called Night. And there was evening and there was morning, the first day. 6 And God said, “Let there be an expanse1 in the midst of the waters, and let it separate the waters from the waters.” 7 And God made2 the expanse and separated the waters that were under the expanse from the waters that were above the expanse. And it was so. 8 And God called the expanse Heaven.3 And there was evening and there was morning, the second day. 9 And God said, “Let the waters under the heavens be gathered together into one place, and let the dry land appear.” And it was so. 10 God called the dry land Earth,4 and the waters that were gathered together he called Seas. And God saw that it was good."; echo $string; $split =preg_replace('/\d+\s+/','<br>',$string); echo $split;
Из этого вы можете получить массив вашей другой строки, теперь перейдите к этому и распечатайте в новой строке.
$pattern = "/(\d)/"; $array = array_filter(preg_split($pattern, $string)); print_r($array);
Также вы можете получить прямой вывод с помощью preg_replace
. Но ведущее пространство там.
echo preg_replace($pattern,'<br/>',$string);
Используйте preg_replace
:
$replaced = preg_replace('/\d+/', "\r\n", $string);
См. https://3v4l.org/D5eNc .
Обратите внимание, однако, что вы, вероятно, захотите заменить дополнительное пустое пространство до и после, так что это может быть лучшим выбором:
$replaced = preg_replace('/\s*\d+\s*/', "\r\n", $string);
См. https://3v4l.org/5Xv10 .
Для справки см. http://php.net/manual/en/function.preg-replace.php .