Create paragraph after 2nd full stop

Hi I’m creating a website and I have news page which I have used php to get the information then post it onto a different page. At the moment I have news page with just the headings and little info. Then when I click to read more the full story appears on another page using a GET method. I am able to do this using a database however my only problem is when I want to read the full story there are no paragraphs which doesn’t look great.

Is there a way in PHP of finding every second full stop in a block of writing and then creating a paragraph after it?

I would think so, but that is not how paragraphs are structured. It is not just a case of making a break on every second sentence. Paragraphs are groups of sentences that belong together. Creating them in such a rigid procedural way, will most likely not read well.
Surely the paragraphs should be marked up in some way when the content is written.

Thanks for your reply. It isn’t the best way I know to layout a paragraph but I was just wondering if there was a way to do so then maybe I could workout out another way after.

One idea is to use explode on the text with '. ’ as the delimiter to create an array of sentences.
Then a foreach with a conditional to test if the array key is odd or even and add <p> tags accordingly, not forgetting to re-add full stops.

if(is_int($key / 2)) {...} // Even numbers
1 Like

Just to clarify that post above is to explode on a full-stop and a space rather than just a full-stop. I was about to post about issues if the text includes a currency amount or anything else that might legitimately contain a full-stop but not be a sentence end before I noticed.

you need to find first full stop , it is your first step , after it , you have to find second full stop , step three is to explode your content after the second full stop ,
ok ,
for finding the first full stop , do this :

$pos = strpos($mystring, $findme);

if ($pos === true) {
$secondpos= strpos($secondpos[1], $findme);

if ($secondpos=== true) {
echo $res[1];//your result 



Something like:-

$sentences = explode('. ', $content);
foreach($sentences as $key => $sent) {
    if(is_int($key / 2)) { echo "<p>".$sent.". " ; $odd = true;} // Even numbers, start para
    else{ echo $sent.".</p>" ; $odd = null;} // Odd numbers, end para
if($odd) { echo "</p>";} // close para if odd number of sentences.

But not tested, just an idea.

just to mention it, an ellipsis (...) would also count as a full stop using that logic.

If typed as three full stops, rather than an actual ellipsis … as a single character &#8230; which is not uncommon. Though in the end, I stand by my first answer, the initial idea is flawed.


Though it could be a fun exercise, it doesn’t have any practical value that I can think of to arbitrarily group sentences into artificial paragraphs based on the number of sentences.

I have a feeling this is an approach to a problem that could be better dealt with.

i.e. if the problem is that rendered HTML does not display source newlines as newlines then nl2br() should probably be used somewhere.

True, using sequential br tags to display groups of sentences as paragraphs is not as semantic as using actual p tags. But at least the sentences would be grouped by their “central theme”.


You can do this using a regular expression like

echo preg_replace('/[^\.]+\.+([^\.]+\.+)?\s?/', '<p>$0</p>', $string);

You could also wrap the text into paragraphs after each line feed like

echo preg_replace('/(.+)\n*/', '<p>$1</p>', $string);

But why not include the markup in those database entries from the first?

1 Like

only if it were incorrectly coded as three full stors instead of as an ellipsis - … - that’s &hellip; in HTML

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.