tags:

views:

58

answers:

6

Hi,

I have a mySql database i want to display only 10 words from its body field which contain html codes, How i can do that, is there any php function to do that.

A: 
$ten = 10;
$text = strip_tags($bodyText);  // remove html tags from the body text
$wordArray = str_word_count($text,2); //extract word offsets into an array
$offsetArray = array_keys($wordArray); // Convert offsets to an array indexed by word
$firstTenWords = substr($text,0,$offsetArray[$ten]-1); extract from the string between the start and tenth word
Mark Baker
A: 

I forgot that you wanted to get the first 10 words but I would try using a substring of a stripped string and bring back a certain number of characters. Probably easier code and relatively the same result:

 <?php 
 $start_position = 0;
 $length = 30; // number of characters, not words
 $suffix = "..."

 // check if string is longer than limit and if so, shorten and attach suffix
 if (strlen($your_text) > ($length - 3) {
   echo substr(strip_tags($your_text), $start_position, $length) . $suffix;
 } else {
   echo $strip_tags($your_text);
 } 
 ?>

Should do the trick if you're getting rid of all formatting, like line breaks, etc.

Josh Pinter
A: 

As your field contains html, it's very difficult to get valid html out - mysql doesn't understand html.

You can use mysql's substring though:

simendsjo
+1  A: 

I would suggest to create one more column for this so you don't need to limit words on every request. Limitation will be easy to do with php:

$str = '<html>word word <b> word word word word word</b> word word word <u> word</u></html>';
$str = strip_tags($str); // strip html tags
preg_match('/^\s*+(?:\S++\s*+){1,10}/u', $str, $matches); // kohana's Text::limit_words()
$str = trim($matches[0]); // first 10 words of string
Anpher
+1 for using regexp
Mark Baker
A: 

something like this might be handy.

 echo substr($returnedQuery, 0,10);
Oliver Bayes-Shelton
OP is after 10 words, not 10 characters
Mark Baker
I didn't say it was the exact answer just it might come in handy.
Oliver Bayes-Shelton
A: 

BETTER solution

function gen_string($string,$min=10,$clean=false) {
    $string = str_replace('<br />',' ',$string);
    $string = str_replace('</p>',' ',$string);
    $string = str_replace('<li>',' ',$string);
    $string = str_replace('</li>',' ',$string);
    $text = trim(strip_tags($string));
    if(strlen($text)>$min) {
        $blank = strpos($text,' ');
        if($blank) {
            # limit plus last word
            $extra = strpos(substr($text,$min),' ');
            $max = $min+$extra;
            $r = substr($text,0,$max);
            if(strlen($text)>=$max && !$clean) $r=trim($r,'.').'...';
        } else {
            # if there are no spaces
            $r = substr($text,0,$min).'...';
        }
    } else {
        # if original length is lower than limit
        $r = $text;
    }
    return trim($r);
}

just pass the html through the gen_string() function

andufo
Tabs, linebreaks, just to name a few why you shouldn't shout 'BETTER'.
Wrikken
tabs and linebreaks don't get displayed on html mode. it is better.
andufo
Thank you its works
saquib