Jump to content

Recommended Posts

So far phpfreaks is 3 for 3 on solving my problems, so here we go again, hopefully its 4 for 4.

 

I want to pull some info from a mysql DB and what I'm looking to do is to display only a snippet of text from a text field (ie: the first 30 words of the text entry).  Can anyone suggest how I might go about doing this?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/
Share on other sites

try

<?php
$text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nulla lacinia justo at velit. Nunc porttitor nisl porta mi. Curabitur sollicitudin purus vel felis. Maecenas nunc. Nulla facilisi. Integer vel ante sit amet ipsum consequat facilisis. Suspendisse hendrerit urna vel erat. Quisque auctor, erat nec ullamcorper sollicitudin, wisi leo hendrerit nisl, eu auctor turpis justo quis justo. Proin arcu metus, commodo quis, tincidunt varius, fermentum eget, lectus. Ut tellus. Sed nec dolor. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nam sed magna.";

$first30 = join (' ', array_slice (explode(' ', $text), 0, 30));

echo $first30;    
?>

I see that this thread is solved but I'm a fan of the substr_replace() function.   

 

$example = "This line has more than 20 characters"

 

echo substr_replace($example, "...", 20);  would print:

 

This line has more t...

 

you can place the 3 periods with anything you want or leave it blank, " ", and use how ever many characters you want to display.  Its a pretty useful function.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.