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 (42) Add New Comment
dewascatter link alternatif2024-03-03 6:19 PM

Hello! Do you know if they make any plugins to assist with SEO? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good gains. If you know of any please share. Appreciate it!

Reply
mpo slot terbaru2024-03-03 3:22 PM

Hello colleagues, how is all, and what you want to say about this article, in my view its really awesome for me.

Reply
dewascatter2024-03-03 12:22 PM

My brother recommended I would possibly like this blog. He was totally right. This publish actually made my day. You cann't consider just how a lot time I had spent for this information! Thanks!

Reply
website design kuala lumpur2024-03-03 6:09 AM

Hi, Neat post. There is an issue with your site in internet explorer, might check this? IE nonetheless is the market leader and a large element of people will pass over your wonderful writing due to this problem.

Reply
dewascatter2024-03-03 5:54 AM

We absolutely love your blog and find almost all of your post's to be exactly what I'm looking for. Would you offer guest writers to write content for yourself? I wouldn't mind publishing a post or elaborating on a few of the subjects you write with regards to here. Again, awesome site!

Reply
pyramidal backlink scheme2024-03-02 11:00 PM

Quality content is the key to interest the users to visit the web page, that's what this web site is providing.

Reply
agen mpo slot2024-03-02 7:44 PM

Wow, awesome blog structure! How long have you ever been blogging for? you made running a blog look easy. The entire glance of your site is magnificent, let alone the content!

Reply
najlepszy sklep2024-03-02 6:23 PM

Today, I went to the beach with my kids. I found a sea shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone! I saw similar here: sklep online and also here: najlepszy sklep

Reply
najlepszy sklep2024-03-02 6:21 PM

I'm really inspired with your writing abilities and also with the structure for your weblog. Is that this a paid subject matter or did you customize it yourself? Either way keep up the nice high quality writing, it's uncommon to look a great weblog like this one nowadays.. I saw similar here: najlepszy sklep and also here: najlepszy sklep

Reply
online casino api2024-03-02 3:15 PM

Our confirmed expertise helps companies ship transactional, OTP and promotional messages to the consumers, securely and on time. Completing the applying topology of an utility allows for straightforward performance correlation across the appliance stack and helps reduce troubleshooting time. Automation for Test Suites: It permits for the automation of take a look at suites, making it a wonderful selection for regression testing and steady integration workflows. Where you do a C synchronous operation, like we had before simply insert one, you are actually doing a blocking call, where you're simply you're making it so that this methodology just stops there, nothing else can occur until the call comes again into the method, right, in this case from the database. So by doing async, here, just next to process after which await right here, when we made the decision, we're sort of adding slightly bit of a syntactic sugar around the whole methodology to inform a dotnet 5, the compiler that this is going to be an asynchronous call.

Reply
dewascatter2024-03-02 2:10 PM

Hello there, I found your blog by the use of Google whilst searching for a similar topic, your web site came up, it seems good. I have bookmarked it in my google bookmarks. Hi there, just changed into alert to your blog via Google, and located that it's really informative. I'm going to watch out for brussels. I'll be grateful for those who continue this in future. Numerous other people will likely be benefited from your writing. Cheers!

Reply
link alternatif mpo slot2024-03-02 1:10 PM

Thanks in support of sharing such a good opinion, article is pleasant, thats why i have read it completely

Reply
motorcycle2024-03-02 11:43 AM

Hi, its fastidious piece of writing regarding media print, we all be aware of media is a impressive source of data.

Reply
dewa scatte2024-03-02 9:23 AM

Today, I went to the beachfront with my children. I found a sea shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!

Reply
dewascatter2024-03-02 5:39 AM

My partner and I stumbled over here different web page and thought I might check things out. I like what I see so now i am following you. Look forward to exploring your web page again.

Reply
mpo slot2024-03-01 9:40 PM

Thanks in favor of sharing such a good thought, post is fastidious, thats why i have read it completely

Reply
link mpo slot2024-03-01 8:44 PM

I enjoy what you guys tend to be up too. This type of clever work and coverage! Keep up the terrific works guys I've added you guys to my blogroll.

Reply
mpo slot2024-03-01 4:35 PM

Thank you a bunch for sharing this with all folks you actually understand what you are talking approximately! Bookmarked. Kindly also consult with my site =). We can have a link exchange arrangement among us

Reply
link mpo slot2024-03-01 2:51 PM

Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

Reply
dewascatter link alternatif2024-03-01 1:42 PM

Just desire to say your article is as astounding. The clearness in your submit is just spectacular and i can suppose you are a professional on this subject. Well together with your permission let me to clutch your feed to stay up to date with impending post. Thanks a million and please carry on the rewarding work.

Reply
link mpo slot2024-03-01 1:13 PM

When someone writes an article he/she keeps the image of a user in his/her mind that how a user can understand it. Therefore that's why this paragraph is outstdanding. Thanks!

Reply
dewa scatter2024-03-01 1:07 PM

Heya! I realize this is sort of off-topic but I needed to ask. Does operating a well-established blog such as yours take a large amount of work? I am completely new to running a blog but I do write in my journal everyday. I'd like to start a blog so I can easily share my own experience and feelings online. Please let me know if you have any kind of recommendations or tips for new aspiring blog owners. Appreciate it!

Reply
reliable football prediction2024-02-28 2:28 PM

Great delivery. Outstanding arguments. Keep up the amazing spirit.

Reply
slot deposit goceng2024-02-27 11:02 PM

Right now it appears like Expression Engine is the preferred blogging platform out there right now. (from what I've read) Is that what you're using on your blog?

Reply
Related Site2024-02-27 5:57 PM

Link exchange is nothing else however it is just placing the other person's weblog link on your page at appropriate place and other person will also do similar in favor of you.

Reply

??, I re?d yo?r new stuff like ever? week. Your writing style is awesome, keep do?ng what yo?'re doing!

Reply
slotxo2024-02-27 6:47 AM

Hi, Neat post. There's a problem with your web site in internet explorer, might test this? IE nonetheless is the market chief and a large part of other people will omit your fantastic writing due to this problem.

Reply
judi slot ovo2024-02-26 12:05 PM

Very nice write-up. I absolutely love this site. Keep it up!

Reply
betting site2024-02-26 11:12 AM

Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your website? My blog is in the very same niche as yours and my users would genuinely benefit from a lot of the information you present here. Please let me know if this ok with you. Appreciate it!

Reply
mpo slot terbaru2024-02-26 10:12 AM

Great article! We are linking to this great article on our website. Keep up the great writing.

Reply
dewascatter link alternatif2024-02-26 9:57 AM

Hi mates, nice post and nice arguments commented here, I am truly enjoying by these.

Reply
dewascatter2024-02-25 7:03 PM

Now I am ready to do my breakfast, after having my breakfast coming yet again to read further news.

Reply

Thanks for every other informative website. The place else may I am getting that type of information written in such an ideal manner? I've a venture that I'm just now operating on, and I've been at the glance out for such information.

Reply
naught2024-02-25 9:32 AM

I could not resist commenting. Exceptionally well writt?n!

Reply
slot mpo terbaru2024-02-25 6:03 AM

I'll right away grasp your rss feed as I can not to find your email subscription link or e-newsletter service. Do you have any? Kindly allow me realize in order that I may just subscribe. Thanks.

Reply
pharma articles2024-02-25 12:40 AM

Wow that was odd. I just wrote an incredibly long comment but after I clicked submit my comment didn't appear. Grrrr... well I'm not writing all that over again. Anyhow, just wanted to say superb blog!

Reply
oddschecker2024-02-24 9:34 PM

Wow, awesome blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is magnificent, as well as the content!

Reply
mpo slot terbaru2024-02-24 1:51 PM

Hello there! This blog post couldn't be written much better! Looking through this post reminds me of my previous roommate! He constantly kept talking about this. I will send this post to him. Fairly certain he will have a great read. Thanks for sharing!

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
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