Skip to content
Advertisement

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

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement