2016-03-07 5 views
0

У меня есть строка, которая содержит текст, как это:Добавить новую строку перед каждым номером в строке

$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."; 

Что я хочу является \ г \ п (новая линия) для любого числа/стиха.

+2

Что относительно «expanse1»? – C2486

ответ

2
$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. 
+0

Спасибо, я забыл сказать, что хочу сохранить контент в текстовом файле.
- это неправильное решение для меня, но действительно будет работать, если HTML –

+2

Я думаю, что он хочет сделать это так, но не спросил правильно. – C2486

+1

, и я предполагаю, что ведущее число должно быть сохранено. – mitkosoft

0

Использование 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.

+0

Спасибо! Именно то, что мне нужно. Он работает и выполняет эту работу для меня. Будьте благословлены;) –

+0

это решение не сохраняет ведущее число каждой строки. – mitkosoft

1

Вы можете использовать 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; 
1

Из этого вы можете получить массив своей различной строки, теперь пройдете эту трассировку и напечатайте в новой строке.

$pattern = "/(\d)/"; 

$array = array_filter(preg_split($pattern, $string)); 
print_r($array); 

Также вы можете получить прямой выход с помощью preg_replace. Но ведущее пространство там.

echo preg_replace($pattern,'<br/>',$string); 
Смежные вопросы