Reputation: 39
I have a string that contains text like this:
$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.";
What i want is a \r\n (new line) for every number/verse.
Upvotes: 0
Views: 329
Reputation: 5316
$regex = '/\d*+\s+(?=[0-9])/';
$string = preg_replace($regex, '<br>', $string); //for HTML output
$string = preg_replace($regex, '\r\n', $string); //for txt file
This will preserve leading number of each row. Output:
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.
Upvotes: 2
Reputation: 9582
Use preg_replace
:
$replaced = preg_replace('/\d+/', "\r\n", $string);
Note, though, that you probably want to replace optional white space before and after, too, so this might be a better choice:
$replaced = preg_replace('/\s*\d+\s*/', "\r\n", $string);
For reference, see http://php.net/manual/en/function.preg-replace.php.
Upvotes: 0
Reputation: 9583
From this you can get the array of your different string, now traverse this and print in new line.
$pattern = "/(\d)/";
$array = array_filter(preg_split($pattern, $string));
print_r($array);
Also you can get the direct output by using preg_replace
. But the leading space in there.
echo preg_replace($pattern,'<br/>',$string);
Upvotes: 1
Reputation: 2642
You can use preg_replace() and 'br'. Below code may help you.
$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;
Upvotes: 1