Display only part of the text in an Html page

Asked

Viewed 558 times

0

I would like to display only part of a String that contains a text. Make a "Click to read more", I tried with

substr

but since I don’t know what character a word ends in, he cuts the text in the middle of the words, I wanted him to cut the text but leave the words whole. I tried to make

explode()

and take a part only and then

implode()

but that didn’t work because it got all messed up the text. If anyone can help. My site is using Laravel PHP

  • Idea: you can use substring, but you have to find out where the word ends. You can think of a function like "I want about 200 characters, if in the 200th character I’m in the middle of a word, take a little more characters until you get to the end of the word and return me the index of that". With this index you can call to subst. Maybe you find the regex useful for this, but need not.

3 answers

1

if(strlen($string) > 100){
    $string = substr($string, 0, 100) . " <a href='#'>Leia Mais ...</a>";
    echo $string;
} else {
    echo $string;
}

Change the Variable String for your Variable.
Value 100 is the number of Characters that will be allowed to appear in the application.

1

using Jquery

You could create a div with the contents of "read more", pass an ID to her, and then first set her as Hide (hidden), which can be with the class bootstrap hiddendiv or jquery id:

<script>$('#id_do_conteudo_do_ler_mais').hide()</script>

soon after:

    $(document).on('click', '#id_do_ler_mais', function() {
 $('#id_do_conteudo_do_ler_mais').show() //isso vai exibir no click do ler mais
});

0

Depending on the version of Larable that you are using, there is a helper for this called str_limit(). More information here

$text = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s';

echo str_limit( $text, 20 ); 

This function will "print" only the first 20 characters and automatically at the end of the string is added the "..."

Browser other questions tagged

You are not signed in. Login or sign up in order to post.