Link to home
Start Free TrialLog in
Avatar of joomla
joomlaFlag for Australia

asked on

First X words in formatted string

function first_words($string, $num)
{
        $words = str_word_count($string, 2);
        $firstwords = array_slice( $words, 0, $num);
        return  implode(' ', $firstwords);
}

I use this PHP function to get the first X number of words from a field in a mysql table.
In an ideal world it works, however if the field contains html tags "<p>" etc... they are included in the string that is returned.

I want to ignore all formatting and simply get the first 10 words in the field
ASKER CERTIFIED SOLUTION
Avatar of Derokorian
Derokorian
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joomla

ASKER

perfect
thank you