How to find out if there is anymore words after a full stop?

0

Issue

I have some text’s stored in the database and while I show them in the frontend I place the words before each full-stop within a li tag.

<ol><li> {!! str_replace(".","</li><li>", $sscat->inclusion) !!} </li></ol>

Everything is working fine except when the text has a full-stop at the very end, it ends up showing an empty numbering.
For example, if I have "aaaaaa.Something Something." the output will be like-

  1. aaaaaa
  2. Something Something

So What can I do to prevent that empty list?

Solution

If you want to show a trailing . in the last list item, you can use preg_replace instead of str_replace to avoid replacing that one if it’s present.

preg_replace("/\.(?!$)/", "</li><li>", $sscat->inclusion)

If you don’t want to show it, aynber’s answer looks good to me, but if you’d rather not use a loop, you can also just trim it off before doing the replacements.

str_replace(".","</li><li>", rtrim($sscat->inclusion, '.'))

Another possibility you might want to consider is modifying the model to handle trailing .s in the inclusion accessor, so you won’t have to do this again if you have other templates that use that property.

Answered By – Don't Panic

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More