• Welcome to AcornDomains.co.uk, a friendly community for UK domain investors, developers and anyone interested in domain names.

    Whether you're looking to buy or sell domains, learn more about the UK domain market, get feedback on your names, or simply chat with other domainers, you're in the right place. AcornDomains has been bringing together people from across the UK domain industry for many years, from complete beginners to experienced investors.

    Have a look around, join the discussions and, if you haven't already, create a free account and introduce yourself.

PHP help

Status
Not open for further replies.
Joined
Mar 13, 2005
Posts
4,660
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.
Top Bottom