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.
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=explode('.',$pos);
$secondpos= strpos($secondpos[1], $findme);
if ($secondpos=== true) {
$res=explode('.',$secondpos);
echo $res[1];//your result
}
}
If typed as three full stops, rather than an actual ellipsis … as a single character … 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”.