Php 在字符串中的每个数字之前添加新行

Php 在字符串中的每个数字之前添加新行,php,Php,我有一个包含如下文本的字符串: $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 s

我有一个包含如下文本的字符串:

$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(新行)。

使用
preg\u replace

$replaced = preg_replace('/\d+/', "\r\n", $string);

但是请注意,您可能也希望在前后替换可选的空白,因此这可能是一个更好的选择:

$replaced = preg_replace('/\s*\d+\s*/', "\r\n", $string);


有关参考,请参阅

使用
preg\u replace

$replaced = preg_replace('/\d+/', "\r\n", $string);

但是请注意,您可能也希望在前后替换可选的空白,因此这可能是一个更好的选择:

$replaced = preg_replace('/\s*\d+\s*/', "\r\n", $string);

有关参考,请参阅

您可以使用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;
起初,上帝创造了天地。2地球没有形状和虚空,黑暗笼罩着深渊的表面。上帝的灵在水面上盘旋。 3神说:“要有光”,就有了光。4神看见光是好的,就把光与黑暗分开。5神称光为昼,称暗为夜。有晚上,有早晨,是第一天。 6神说:“水中间要有一块空地,把水与水隔开。”7神造了那广阔之地,将那广阔之地以下的水和那广阔之地以上的水分开。8神称那广阔之地为天。3有晚上,有早晨,是第二天。 神说:“天下的水要聚集在一处,使旱地显露出来。”于是,神把旱地称为地,把聚集的水称为海。神看见这是好的; echo$字符串; $split=preg_replace('/\d+\s+/','
',$string); echo$split; 您可以使用preg\u 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;
起初,上帝创造了天地。2地球没有形状和虚空,黑暗笼罩着深渊的表面。上帝的灵在水面上盘旋。 3神说:“要有光”,就有了光。4神看见光是好的,就把光与黑暗分开。5神称光为昼,称暗为夜。有晚上,有早晨,是第一天。 6神说:“水中间要有一块空地,把水与水隔开。”7神造了那广阔之地,将那广阔之地以下的水和那广阔之地以上的水分开。8神称那广阔之地为天。3有晚上,有早晨,是第二天。 神说:“天下的水要聚集在一处,使旱地显露出来。”于是,神把旱地称为地,把聚集的水称为海。神看见这是好的; echo$字符串; $split=preg_replace('/\d+\s+/','
',$string); echo$split;
从中,您可以获得不同字符串的数组,现在遍历此数组并在新行中打印

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

$array = array_filter(preg_split($pattern, $string));
print_r($array);
您还可以使用
preg\u replace
获得直接输出。但这里的主导空间

echo preg_replace($pattern,'<br/>',$string);
echo preg_替换($pattern,
,$string);
从中,您可以获得不同字符串的数组,现在遍历此数组并在新行中打印

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

$array = array_filter(preg_split($pattern, $string));
print_r($array);
您还可以使用
preg\u replace
获得直接输出。但这里的主导空间

echo preg_replace($pattern,'<br/>',$string);
echo preg_替换($pattern,
,$string);
关于“expanse1”呢?关于“expanse1”呢?谢谢,我忘了告诉你我想将内容保存到文本文件中
对我来说不是正确的解决方案,但如果HTMLI认为他想这样做,但没有以正确的方式询问,则确实会起作用。更新后,我假设必须保留前导号码。谢谢,我忘了告诉他我想将内容保存到文本文件中
对我来说不是正确的解决方案,但如果HTMLI认为他想这样做,但没有以正确的方式询问,则确实会起作用。更新,我认为必须保留前导号码。谢谢!正是我需要的。它为我工作。祝福;)此解决方案不会保留每行的前导编号。谢谢!正是我需要的。它为我工作。祝福;)此解决方案不保留每行的前导编号。