How to return only part of a feild from database using php substr but to take into account spaces

I recently needed to get data from a mysql data base but only wanted to return the first 35 characters, but did not want it to cut off in the middle of a word so wanted to make sure that it checked for a space after that and cut off after that space.

Firstly though I had to check that there were enough characters otherwise it would return a false, also if the cutoff point was the last word it would also cut off as it would not find a space, so would return a blank, so in both cases I used the original text as that was fine.

Otherwise it then used the formula below, change len to be the number of characters you want to return. If you have a set number and cant exceed that number just make your $len about 15 lower than you want that should always return less.

 


Trending right now

Stop paying council tax and remove yourself from their database

How to Transform Your Product into a Coveted Luxury Item

How to mute keywords in twitter and hide them from your timeline

Why Speedy Lead Calls Skyrocket Your Success

How to stop people stealing your downloads when using paypal

Why Letting Go is the Key to Scaling Your Business

How people are losing thousands of pounds when they thought they had their details safe.

How to stop posts from facebook groups showing in my timeline.

Promissory Notes and Bills of Exchange

Foods to avoid during H pylori infection

Why Blind Faith Leads to Catastrophe

Natural Remedies and Dietary Approach for Psoriasis

Disney frozen bath towel very good condition

Why Your Business Must Avoid the Trap of a Single Supplier

How to remove inspyware from your laptop

How to stop google chrome downloading automatically

Single duvet featuring caticorn ie cat like a uncorn white and pink

What have we learnt about humanity since covid appeared.

H and M 10 percent discount code

How to make money from your website by doing very little

Why do people actually sell on ebay

Forty five great places to leave your advertising material that will actually get read

I will promote your ebay sellers page

Why Every Customer Deserves Your VIP Treatment

Your unalienable rights

How to protect yourself easily from covid

How to remove item specifics on ebay

How to change your name on facebook

Why Ditching the News Could Transform Your Mental Health

Some things I am selling please have a look

Boost your business Promote your site here for free

A miracle is on its way to you right now

Are you a commercial electrical company

Heal arthritis with this super simple method. You probably have some of this in your cupboard

Share