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

时间:2016-03-07 12:39:15

标签: 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 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(新行)。

4 个答案:

答案 0 :(得分: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.

答案 1 :(得分:1)

您可以使用preg_replace()和&#39; br&#39;。下面的代码可以帮助您。

 $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 :(得分:1)

从这里你可以获得不同字符串的数组,现在遍历它并在新行中打印。

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

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

您也可以使用preg_replace获得直接输出。但那里的领先空间。

echo preg_replace($pattern,'<br/>',$string);

答案 3 :(得分: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