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.
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.
$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
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.
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
something like this might be handy.
echo substr($returnedQuery, 0,10);
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