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 (19) Add New Comment
web page2024-02-23 8:32 AM

Hello to every single one, it's genuinely a pleasant for me to pay a visit this web page, it consists of valuable Information.

Reply
Garden care2024-02-23 6:33 AM

We're a group of volunteers and starting a brand new scheme in our community. Your website offered us with useful information to work on. You've done an impressive job and our whole group shall be grateful to you.

Reply
Krystle2024-02-22 11:42 PM

Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! However, how can we communicate?

Reply
dewascatter link2024-02-22 2:57 AM

of course like your website however you need to take a look at the spelling on quite a few of your posts. Many of them are rife with spelling issues and I to find it very troublesome to tell the truth however I'll definitely come back again.

Reply
????????? ?????2024-02-22 1:29 AM

I enjoy looking through an article that will make men and women think. Also, many thanks for allowing for me to comment!

Reply
Roseann2024-02-20 6:10 PM

Thanks for your marvelous posting! I really enjoyed reading it, you can be a great author.I will remember to bookmark your blog and will come back later on. I want to encourage continue your great job, have a nice morning!

Reply
????????????????2024-02-20 12:28 PM

Hello there! Do you know if they make any plugins to protect against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any tips?

Reply
find more2024-02-19 11:06 PM

My family members all the time say that I am wasting my time here at web, however I know I am getting experience daily by reading such good articles.

Reply
find more2024-02-19 11:06 PM

My family members all the time say that I am wasting my time here at web, however I know I am getting experience daily by reading such good articles.

Reply
???????? fun882024-02-19 5:56 AM

Howdy! I could have sworn I've visited this blog before but after looking at a few of the posts I realized it's new to me. Regardless, I'm certainly happy I came across it and I'll be bookmarking it and checking back frequently!

Reply
????????? ???????2024-02-18 11:31 PM

Hurrah! In the end I got a weblog from where I be able to actually get valuable facts regarding my study and knowledge.

Reply
????????????2024-02-15 12:36 PM

Hello, i believe that i noticed you visited my blog so i came to return the want?.I'm trying to in finding issues to enhance my website!I assume its ok to use a few of your concepts!!

Reply
????????????2024-02-15 10:27 AM

This post is priceless. How can I find out more?

Reply
???????????2024-02-12 11:42 PM

Hi there are using Wordpress for your site platform? I'm new to the blog world but I'm trying to get started and set up my own. Do you require any coding expertise to make your own blog? Any help would be really appreciated!

Reply
Rubbish Clearance2024-02-12 8:12 PM

I'd like to thank you for the efforts you have put in writing this blog. I really hope to see the same high-grade blog posts from you later on as well. In truth, your creative writing abilities has motivated me to get my own website now ;)

Reply
?????2024-02-08 7:56 PM

Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a little bit, but instead of that, this is magnificent blog. A great read. I will definitely be back.

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
Calculator Online advanced2024-02-07 4:27 AM

Hello, yes this paragraph is genuinely fastidious and I have learned lot of things from it concerning blogging. thanks.

Reply