2016-03-07 20 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."; 

我要的是\ r \ n(新行)。

+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);