2019-10-07 Pujan Niroula 1 Minute(s) Read

[How to] Create Snippet Without Breaking Word in PHP

img-substr long text wo breaking word 1.jpg

Hello there, I am back with new script to create snippet of long text without breaking word.

Recently I have been working on a friend's site he asked me to make create snippet of blog post just like this blog.

I created it like

<?php
//Long text to break
$long_text = "Let's use PHP Substr to break the word to create snippet of certain length of a very long text.";
$snippet = substr($long_text, 0, 50);
echo $snippet;
?>

Substr Long String Without Breaking a Word

 This script will break the text after 30 characters but he doesn't want to break word so asked me to fix it. I never thought about that but I have to create this. After some googling I found perfect method to do so.

<?php
$text = "This is long text to be broken into short snippest just like this blog but little bit advance which won't break any word. Just Check this out and keep visiting this blog for more cool tricks and tips";
$length = 10;
$max_length = 30; //Set Maxlength of last word so that a long very long single word to be broken

if(strlen($text)<=$length){
    echo substr($text, 0, $length);
} else {
    for ($i=0; $i<=$max_length; $i++) {

        if($text[$length+$i]==' ' OR $i==$max_length){

            echo substr($text, 0, $length+$i);
            break;    
        }
    }
}
?>

Furthermore, I have added $max_length variable so that if a user submit a long word it will break and create snippet. I think practically there is no word longer than 30 character.

Thanks for reading, and happy coding.

Comments (10) Add New Comment
Louistheok2024-10-10 3:16 AM

???????? ???????-??????????? ?????????????????? ???????-??????????? ?????????? ??? ???????????? ?????? ?????????? ?????? ???????????? ????????, ????? ?????, ???????, ?????????????? ?????????? (????????? ? ?????????) ? ?.?. ???????? ?????????? ?????????????????? ???????????????????? ?????????? ???????????? ?????? ???????????????? ?? ??????????? ???????? ?? ???????????? ??????????? ??????? ? ???????????? ???????? ? ???????? ?????????? ???????????????? ? ?????????? ?????????. ???????? ?????????? ????????????? ?????????????? ?????????? ???????????? ???????? ??? ???????????? ????????? ? ?????? ????????????? ??????. ???????? ?????????? ??????? ? ?????????????????? ?????????? ??????? ? ?????????? ?? ??????? ??????????? ????????? ?????????? ? ??????? ? ??????????? ?? ????????????? ? ??????????????. ???????? ?????????? ???????????? ?????????????? ? ??????-????????? ????????????????? - ?????????? ???????????? ?????????????? ? ??????-????????? ?????????? ?? ??????? ?? ??????? ?? ????? ????????????? ??????.

Reply
StephenCulse2024-10-04 8:48 AM

??????? ? ?????? ???????? ?????????? (? ??????? ????????? ??????-????????????????? ???????? ???????? ?????????) ? 1977 ????, ???? ???????? ??????? ?? ??????? ?? ???????? ??????? ???? ? ?????????? ?????????. ??? ??????????? ???? ?????? ( 45 ???) ? ????????? ?????? ????? ????????? ?? ?????????????? ???????????? ????????? ?????? ??? ?????????? ??????????, ?????????? ??????????? ?????????????, ?????????????? ? ??????? ????????????.

Reply
JamesNub2024-10-04 5:34 AM

??????? ???????????????? ?? ??????????? ? ????????? ?????, ??????? ??????? ??????????? ????? ??????????? ?? ????????? ??????? ???????? ???????-??????????? ??????????. ??? ?????????? ?????? ??????? ??? ?????????? ????? ????????????? ??????????????? ??????? (?????, ??????????? ??????????????? ???????, ????????????), ????????? ????????????? ? ???????? ???????, ? ????? ??????? ?????????????? ??????????, ????? ??? ???? ??????? ?????????? ??????? ?? ????: ? ????????? ??????????? ?? ??????????? ??? ????, ? ?????????????? ?????????????? ? ?????????? ? ??.

Reply
Louistheok2024-10-04 5:22 AM

???????? ???????-??????????? ?????????????????? ???????-??????????? ?????????? ??? ???????????? ?????? ?????????? ?????? ???????????? ????????, ????? ?????, ???????, ?????????????? ?????????? (????????? ? ?????????) ? ?.?. ???????? ?????????? ?????????????????? ???????????????????? ?????????? ???????????? ?????? ???????????????? ?? ??????????? ???????? ?? ???????????? ??????????? ??????? ? ???????????? ???????? ? ???????? ?????????? ???????????????? ? ?????????? ?????????. ???????? ?????????? ????????????? ?????????????? ?????????? ???????????? ???????? ??? ???????????? ????????? ? ?????? ????????????? ??????. ???????? ?????????? ??????? ? ?????????????????? ?????????? ??????? ? ?????????? ?? ??????? ??????????? ????????? ?????????? ? ??????? ? ??????????? ?? ????????????? ? ??????????????. ???????? ?????????? ???????????? ?????????????? ? ??????-????????? ????????????????? - ?????????? ???????????? ?????????????? ? ??????-????????? ?????????? ?? ??????? ?? ??????? ?? ????? ????????????? ??????.

Reply
combed2024-09-26 4:56 AM

Afte? looking at a few of the blog articles on your web sit?, ? trul? appreciate your technique of w?iting a blog. I bookmarked it to my bookmark webpag? list and will be checking ba?k soon. P?ease visit my website too and tell m? how you feel.

Reply
MartinsAlOmOn2024-09-15 8:36 AM

Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Thanks

Reply
save tube2024-09-15 2:09 AM

Sudahkah Anda mengaktifkan mode penghemat data Instagram untuk mengurangi penggunaan data seluler?

Reply
MartinsAlOmOn2024-09-14 10:36 AM

Thanks for your marvelous posting! I definitely enjoyed reading it, you will be a great author.I will make sure to bookmark your blog and will come back from now on. I want to encourage you to ultimately continue your great work, have a nice evening!

Reply
blog del narco2024-02-08 11:43 AM

Institutions reporting to the central government likely have closer to $six trillion in foreign assets, Setser mentioned.

Reply
web,2024-02-07 4:49 AM

I really like what you guys are usually up too. This sort of clever work and exposure! Keep up the awesome works guys I've incorporated you guys to my personal blogroll.

Reply