Simple HTML problem

Published 7 months ago by finchy70

Trying to output the following on one line but it always renders with the ...read more. on a separate line instead of appending the shortened post. Any ideas?

<div>
     <span class="font-italic">{!! $post->body !!}<a class="read-more" href="/posts/show/{{$post->id}}">{{ (strlen($post->body)>99?"... read more.":"")}}</a></span>
</div>```
Cronix
Cronix
7 months ago (730,880 XP)

Laravel also has a helper for that kind of thing. https://laravel.com/docs/5.5/helpers#method-str-limit

Snapey
Snapey
7 months ago (995,145 XP)

You are outputting the whole post body, which possibly includes a new line at the end.

You just want to output an excerpt from the body

There is a function that will do this for you.

<div>
     <span class="font-italic d-inline-block">{!! str_limit($post->body,99,'...') !!} <a class="read-more" href="/posts/show/{{$post->id}}">read more</a></span>
</div>

You may need to code around having a body that is too short.

You also need to take into account that the 99 characters includes all the html.

Normal approach is to make an excerpt, and you could do this with a model accessor. Create an accessor that returns plain text and uses the limit so that you are getting back just the excerpt

finchy70

Turns out my $post->body was stored in the db with

tags. Used strip_tags($post->body) and everything worked as expected.

Please sign in or create an account to participate in this conversation.