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 (15) Add New Comment
Patti2024-06-23 12:38 PM

Why people still make use of to read news papers when in this technological globe everything is accessible on net?

Reply
Rena2024-06-22 11:12 PM

Hi there, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam remarks? If so how do you protect against it, any plugin or anything you can recommend? I get so much lately it's driving me crazy so any help is very much appreciated.

Reply
Ismaeleromo2024-06-21 8:54 PM

Pretty! This was an extremely wonderful post. Thank you for providing this information. newsofgames.ru/page/28  baotanglichsuvn.com/applied-arts-in-oriental-tea-culture-402.html  zakon122.ru/promotion/  kinogonews.ru/page/5  brisbaneglobaltens.com/tournament/brisbane 

Reply
build blog2024-06-17 10:18 AM

Howdy! Do you know if they make any plugins to assist with Search Engine Optimization? I'm trying to get my site to rank for some targeted keywords but I'm not seeing very good gains. If you know of any please share. Appreciate it! I saw similar art here: blogexpander.com

Reply
guna-travel2024-06-15 1:18 PM

Apakah Anda mencari web judi online yang menawarkan pengalaman bermain terbaik, bonus melimpah, dan keamanan terjamin? Anda telah datang ke daerah yang tepat! Kami menghadirkan platform judi online yang dirancang khusus untuk memenuhi keperluan Anda. Dari permainan kasino klasik sampai taruhan olahraga, kami memiliki semuanya!

Reply
delopodushe2024-06-15 12:37 PM

Selamat datang di web judi online terbaik yang akan memberikan Anda pengalaman bermain yang tidak tertandingi! Dengan bermacam-macam tipe permainan menarik, bonus melimpah, dan pelayanan terbaik, kami hadir untuk memenuhi seluruh keperluan hiburan Anda.

Reply
bstpo2024-06-14 7:03 AM

Selamat datang di dunia permainan judi online yang penuh keseruan dan profit! Situs judi online kami menawarkan pengalaman bermain yang tidak tertandingi dengan pelbagai opsi permainan, bonus menarik, dan layanan pelanggan yang responsif. Mari kita telusuri lebih lanjut kenapa situs kami ialah alternatif terbaik bagi Anda.

Reply
popeyesbeachresort2024-06-14 5:50 AM

Dalam dunia komputerisasi yang terus berkembang, judi online telah menjadi salah satu hiburan paling populer. Dengan banyaknya pilihan situs judi online, penting untuk memilih yang terbaik untuk memutuskan pengalaman bermain yang aman dan menyenangkan. Kami hadir untuk memperkenalkan Anda pada website judi online terbaik yang tidak hanya menawarkan permainan seru, melainkan juga bermacam bonus dan promosi menarik.

Reply
olehova2024-06-13 1:33 PM

Rasakan puncak judi online dengan infrastruktur server terbaik kami. Temukan dunia sensasi mendebarkan, di mana gameplay mulus dan konektivitas tanpa gangguan meningkatkan pengalaman bermain Anda. Server canggih kami menjamin waktu respon yang kencang, menjaga Anda tetap di garis depan tiap taruhan dan putaran. Meresapi diri Anda dalam dunia di mana daya kerja prima bertemu dengan keandalan tanpa tanding. Bergabunglah dengan kami hari ini dan dapatkan kenapa pemain berkelas memilih platform kami untuk petualangan judi online terbaik. Selamat datang pada puncak keunggulan dalam permainan daring.

Reply
StephenAmave2024-06-11 9:42 PM

Simply desire to say your article is as astonishing. The clarity on your publish is simply spectacular and i could suppose you are knowledgeable in this subject. Well with your permission let me to grab your feed to keep updated with coming near near post. Thank you a million and please continue the gratifying work. topbestessays.com/?  socialle.com.br/blogs/23/Where-can-I-buy-a-certificate-or-a-diploma-at?  opaseke.com/users/6782?  remont-okon.od.ua/index.php?subaction=userinfo&user=uqepyd?  la-buket.ru/catalog/?ELEMENT_ID=50&SECTION_ID=2? 

Reply
gladesonline2024-06-11 7:38 AM

Selamat datang di website judi online terpercaya yang menawarkan pengalaman bermain yang aman, nyaman, dan menguntungkan. Kami memahami betapa pentingnya keamanan data dan transaksi Anda, oleh sebab itu kami menerapkan enkripsi tingkat tinggi dan metode keamanan canggih untuk melindungi info pribadi Anda.

Reply
all escape rooms2024-06-09 6:34 AM

Hey there! Do you know if they make any plugins to help with Search Engine Optimization? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good results. If you know of any please share. Thank you! You can read similar blog here: Which escape room

Reply
TimsothyShubs2024-06-08 7:27 PM

What's up to every body, it's my first go to see of this weblog; this weblog consists of amazing and in fact fine information in favor of readers. detki-v-setke.ru/index.php?showuser=21983?  calc-bank.ru/calculator/krayinvestbank?  ros-con.ru/index.html?  horordark.ru/page/8?  dip-vuz.ru/kupit-diplom-v-tyumeni/index.html? 

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