Membership is FREE – with unlimited access to all features, tools, and discussions. Premium accounts get benefits like banner ads and newsletter exposure. ✅ Signature links are now free for all. 🚫 No AI-generated (LLM) posts allowed. Share your own thoughts and experience — accounts may be terminated for violations.

PHP help

Status
Not open for further replies.
Joined
Mar 13, 2005
Posts
4,662
Reaction score
184
Can any PHP geniuses help me out here, here is what I want to do:

if length of $string > 50 then $string = first 50 characters + "....."


Thanking you kindly

Grant
 
$string = (strlen($string)>50) ? substr($string,0,50).'…' : $string;

Jee
 
If you're creating an excerpt then that'll sometimes cut you off in the middle of words.

You can avoid this by trimming the output of the above and removing anything between the last word boundary and the end of the string.

Code:
$string = preg_replace('/\b.*$/', '', $string);

Even smarter excerpts can be created by looking for whole sentences. Though with only 50 characters you probably won't get many of them.
 
Status
Not open for further replies.

Rule #1: Be Respectful

Do not insult any other member. Be polite and do business. Thank you!

Members online

Premium Members

New Threads

Domain Forum Friends

Our Mods' Businesses

*the exceptional businesses of our esteemed moderators
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Siusaidh AcornBot:
    Siusaidh has left the room.
      Siusaidh AcornBot: Siusaidh has left the room.
      Top Bottom