Limit amount of text shown

23 posts by 6 authors in: Forums > CMS Builder
Last Post: November 17, 2010   (RSS)

By Mikey - November 16, 2010 - edited: November 16, 2010

Dave,
I've marked the problem areas red. You'll see when a break <br/> is used to start a new sentence and maxWords is used, there's no space between the last sentence's period and where the new sentence starts. You'll also see the <li> bullet list items are not wrapped in <ul><li>, nor wrapped in <p> as a result of the maxWords code, so the bullet list is loosing all <p> formatting and thereby the font's style does not match the rest of the article.

Here's a sample of the page code:
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

// load viewer library
$libraryPath = 'abc/lib/viewer_functions.php';
$dirsToCheck = array('/home/content/a/b/c/economics/html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records - core page content
list($economicsRecords, $economicsMetaData) = getRecords(array(
'tableName' => 'economics',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$economicsRecord = @$economicsRecords[0]; // get first record

// load a single featured news record used with maxWords
list($economic_newsFeaturedRecords, $economic_newsFeaturedMetaData) = getRecords(array(
'tableName' => 'economic_news',
'where' => "economics_categories",
'limit' => '1',
));

// load 4 news records displaying only their titles and date
list($economic_newsRecords, $economic_newsMetaData) = getRecords(array(
'tableName' => 'economic_news',
//'allowSearch' => false,
'where' => "economics_categories",
'limit' => '4',
));
$economic_newsRecord = @$economic_newsRecords[0]; // get first record

?>
<?PHP
function maxWords($textOrHtml, $maxWords) {
$text=str_replace("<p>","*P*",$textOrHtml);
$text= str_replace("</p>","*/P*",$text);
$text = strip_tags($text);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);
$output=str_replace("*P*","<p>",$output);
$output=str_replace("*/P*","</p>",$output);
$output.="</p>";

return $output;
}
?>
<?PHP
// function maxWords($textOrHtml, $maxWords) {
// $text = strip_tags($textOrHtml);
// $words = preg_split("/\s+/", $text, $maxWords+1);
// if (count($words) > $maxWords) { unset($words[$maxWords]); }
// $output = join(' ', $words);
//
// return $output;
// }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<!-- css -->
<link href="css/bessemercity.css" type="text/css" rel="stylesheet" />
<!-- /css -->
</head>

<body>
<div id="content-container"><!-- content -->
<?php foreach ($economicsRecords as $record): ?>
<h1><?php echo $economicsRecord['title'] ?></h1>
<p><?php echo $record['content'] ?></p>
<?php endforeach; ?><!-- /content -->

<div id="news-column">
<!-- news column -->
<?php foreach ($economic_newsFeaturedRecords as $record): ?>
<h3><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></h3>
<p><?php echo date("F jS, Y", strtotime($record['date'])) ?><br />
<?php echo maxWords($record['content'], 140); ?></p>
<p><a href="<?php echo $record['_link'] ?>">Continue Reading...</a></p><br />
<?php endforeach ?>
<!-- /news column --></div>

<div id="news-titles">
<!-- news titles -->
<?php foreach ($economic_newsRecords as $record): ?>
<p><strong><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></strong><br />
<?php echo date("F jS, Y", strtotime($record['date'])) ?></p>
<?php endforeach ?>
<!-- /news titles -->
</div>

<?php if (!$economicsRecords): ?>
<h3>There are no records.</h3><?php endif ?>

<!-- /content-container --></div>

</body>
</html>


Here's the html code as it was entered into the wysiwyg:
<p><strong>Ommy num quat, voluptat. Del in utpatem nulputatem dolobore molum vulput wiscili siscin hent nullandre magniam ilit nostrud ex eros duipsus cillaore tin ut iniat velenisci essi.</strong><br />Modipsusci blan henisit la feugait lore minisci bla facipsum aliquat. Duisis augiam, velessim digna consenim verat. Minis auguerci eugait ulputat. Putpat. Nullaor sed et, quisl ut ute cortin henibh exeratum ipit veliquam veliquat, quamcor sed tet augait alis nostio consend reetue conse min ute tem zzrit nonum iustrud tie magna feummolore doloreetue tat in utpat numsandipsum do esecte mod dit diam, quat.</p>
<ul>
<li>List item one</li>
<li>List item two</li>
<li>List item three</li>
<li>List item four</li>
</ul>
<p>Ud tationsed tisl dolore feum dolor iurer sum inim ipismod ignibh et laore tisl etuer acil iurem iusto consequ amcommy niamet lum vel ip eliquipit la atummy nummy nisl delis estrud etum et, sum volore et veliquat, commolore commodo lortion sequatisit ipsummy nostrud tat. Iduis eu facilla faccum ero er si.</p>


Here's the HTML I'm getting back on the site page:
<div id="news-column">
<!-- news column -->
<h3><a href="economic_news_details.php?Sample-23">Economics Sample</a></h3>
<p>November 16th, 2010<br />
<p>Ommy num quat, voluptat. Del in utpatem nulputatem dolobore molum vulput wiscili siscin hent nullandre magniam ilit nostrud ex eros duipsus cillaore tin ut iniat velenisci essi.Modipsusci blan henisit la feugait lore minisci bla facipsum aliquat. Duisis augiam, velessim digna consenim verat. Minis auguerci eugait ulputat. Putpat. Nullaor sed et, quisl ut ute cortin henibh exeratum ipit veliquam veliquat, quamcor sed tet augait alis nostio consend reetue conse min ute tem zzrit nonum iustrud tie magna feummolore doloreetue tat in utpat numsandipsum do esecte mod dit diam, quat.</p> List item one List item two List item three List item four <p>Ud tationsed tisl dolore feum dolor iurer sum inim ipismod ignibh et laore tisl etuer acil iurem iusto consequ amcommy niamet lum vel ip eliquipit la atummy nummy nisl delis estrud etum et, sum volore et veliquat, commolore commodo lortion</p></p>

<p><a href="economic_news_details.php?Sample-23">Continue Reading...</a></p><br />

<!-- /news column --></div>


Here's an example of what I'd like to get back as a result of the maxWords and article post. But this would require integrating the <ul> and <li> into the maxWords code I suspect.
<div id="news-column">
<!-- news column -->
<h3><a href="economic_news_details.php?Sample-23">Economics Sample</a></h3>
<p>November 16th, 2010<br />
<p>Ommy num quat, voluptat. Del in utpatem nulputatem dolobore molum vulput wiscili siscin hent nullandre magniam ilit nostrud ex eros duipsus cillaore tin ut iniat velenisci essi.<br />Modipsusci blan henisit la feugait lore minisci bla facipsum aliquat. Duisis augiam, velessim digna consenim verat. Minis auguerci eugait ulputat. Putpat. Nullaor sed et, quisl ut ute cortin henibh exeratum ipit veliquam veliquat, quamcor sed tet augait alis nostio consend reetue conse min ute tem zzrit nonum iustrud tie magna feummolore doloreetue tat in utpat numsandipsum do esecte mod dit diam, quat.</p>
<ul>
<li>List item one</li>
<li>List item two</li>
<li>List item three</li>
<li>List item four</li>
</ul>
<p>Ud tationsed tisl dolore feum dolor iurer sum inim ipismod ignibh et laore tisl etuer acil iurem iusto consequ amcommy niamet lum vel ip eliquipit la atummy nummy nisl delis estrud etum et, sum volore et veliquat, commolore commodo lortion</p></p>

<p><a href="economic_news_details.php?Sample-23">Continue Reading...</a></p><br />

<!-- /news column --></div>


If using the <ul> and <li> in the maxWords is not possible, I'd like to just make the bullet list items change to the match the <p> paragraph so the text matches the rest of the article and flows naturally with the paragraph as if it was entered in the paragraph. And if there's a sentence that I or someone has used a <br/> break before starting the next sentence... the next sentence would have a space in between the previous sentence's period and the beginning of the next sentence.

Here's what I'd like the results to appear if it's not possible to show <ul><li> within the maxWords. You'll see marked in green that there's a space between "essi. Modipsusci" and the "List item one List item two List item three List item four" are within the <p></p> tags and not excluded as they with the present state of the maxWords code.

<div id="news-column">
<!-- news column -->
<h3><a href="economic_news_details.php?Sample-23">Economics Sample</a></h3>
<p>November 16th, 2010<br />
<p>Ommy num quat, voluptat. Del in utpatem nulputatem dolobore molum vulput wiscili siscin hent nullandre magniam ilit nostrud ex eros duipsus cillaore tin ut iniat velenisci essi. Modipsusci blan henisit la feugait lore minisci bla facipsum aliquat. Duisis augiam, velessim digna consenim verat. Minis auguerci eugait ulputat. Putpat. Nullaor sed et, quisl ut ute cortin henibh exeratum ipit veliquam veliquat, quamcor sed tet augait alis nostio consend reetue conse min ute tem zzrit nonum iustrud tie magna feummolore doloreetue tat in utpat numsandipsum do esecte mod dit diam, quat. List item one List item two List item three List item four Ud tationsed tisl dolore feum dolor iurer sum inim ipismod ignibh et laore tisl etuer acil iurem iusto consequ amcommy niamet lum vel ip eliquipit la atummy nummy nisl delis estrud etum et, sum volore et veliquat, commolore commodo lortion</p></p>

<p><a href="economic_news_details.php?Sample-23">Continue Reading...</a></p><br />

<!-- /news column --></div>


Thanks for your help Dave and let me know if there's anything else you need from me.
Zick

Re: [zick] Limit amount of text shown

By Chris - November 17, 2010

Hi zick,

How about converting your <li></li> to <p></p> and making sure there's a space inserted in place of your <br/>?

function maxWords($html, $maxWords) {
$html = str_replace("<li>","<p>",$html);
$html = str_replace("</li>","</p>",$html);
$html = str_replace("<p>","*P*",$html);
$html = str_replace("</p>","*/P*",$html);
$html = str_replace("<"," <",$html);
$text = strip_tags($html);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);
$output=str_replace("*P*","<p>",$output);
$output=str_replace("*/P*","</p>",$output);
$output.="</p>";

return $output;
}


Does that help?
All the best,
Chris