7 posts by 5 authors in: Forums > CMS Builder
Last Post: July 5, 2010   (RSS)

By nmsinc - June 29, 2010

[font "Times New Roman"]I’m looking for a search utility or code that prints a portion of the content description with the words matched and highlighted – similar to that of Google![/#000000]

[font "Times New Roman"] [/#000000]

[font "Times New Roman"]Example Search: [/#000000]

[font "Times New Roman"]Content Text Search = “Jon”[/#000000]

[font "Times New Roman"] [/#000000]

[font "Times New Roman"]Return: [/#000000]
  1. [font "Times New Roman"]Jon John’s fishing hole[/#000000]


[font "Times New Roman"]…Content description. Jon John’s Fishing hole, established January 31, 1910 is one of America’s finest retreats. Bass masters come from every state in the union on July 4th to show the world their craft. Come see….[/#000000]

[font "Times New Roman"] [/#000000]

[font "Times New Roman"]This would be a terrific add-on to the CSM Builder![/#000000]

[font "Times New Roman"] [/#000000]

[font "Times New Roman"]Any help would be appreciated.[/#000000]

Re: [nmsinc] Search Utility

By Jason - June 30, 2010

Hi,

If you have the text you want to search for in a variable, you can do a search/replace on your content.

for example, say you have your content in a field called "description" and your search text in a variable call "search". You could use code like this:

<?php
$content = $record['description'];

$content=str_replace($search,"<b>$search</b>",$content);

echo $content;
?>


What this code does is find's all the instances of the search text in the description field and places <b> tags around them.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [nmsinc] Search Utility

By nmsinc - June 30, 2010

Thanks Jason,



[font "Tahoma"]That would work fine for a content field that contains less then 1,000 characters, ours can contain 5,000 to 20,000 and I do not want the search result to list the entire field, I only need 25 to 30 words within the field that contain the requested word(s) asked for in the search similar to what one would expect to see in the result listing of a Google search – further help would be appreciated.[/#000000]

Ray

Re: [nmsinc] Search Utility

By Jason - June 30, 2010

Hi Ray,

If you want to limit the number of times the search word is highlighted, you can try this:

<?php
$content = $record['description'];
$limit=25;
$content=preg_replace("/$search/","<b>$search</b>",$content,$limit);

echo $content;
?>


You can set the variable $limit to the number of times you want the word to be highlighted.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [nmsinc] Search Utility

By zip222 - July 1, 2010

What about using Google Site Search. I just used it for the first a few weeks back and I am very impressed with it.

http://www.google.com/sitesearch/

It only costs $100 per year, and provides great feedback through google analytics.

Re: [nmsinc] Search Utility

By gkornbluth - July 5, 2010

Hi nmsinc,

You can limit the number of words displayed using something like the maxwords function described in my CMSB Cookbook http://www.thecmsbcookbook.com.

Here's a recipe that might help.

Best,

Jerry Kornbluth

LIMITING THE NUMBER OF WORDS SHOWN IN A PAGE
If you use the character limiting function that’s exactly what you’ll get, and words can be cut off. But if you want to limit the word count, Dave shares his usual uncanny wisdom:

Put this code at the top of your page, or before you want to invoke the word limiting function:

<?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;
}
?>


Then put this code where you want the words limited (to 5 words):

<?PHP echo maxWords($record['content'], 5);
?>
If you want to add ...more and a link, use something like this:

<?PHP echo maxWords($record['content'], 5);
?>...<a href="<?php echo $record['_link']; ?>”>Read More</a>


or

<?PHP echo maxWords($record['content'], 5);
?>...<a class="read-more" href="http://www.your_site.com/your_detail_page.php?<?php echo $record['num'] ?>">(Read More)</a>

To allow certain tags like <br> or <p> to appear in your text, change the strip_tags code:

$text = strip_tags($textOrHtml);

to

$text = strip_tags($textOrHtml, '<br >');


The function is pretty literal, but it seems to automatically include the closing tag, so include all the flavors of the tags you want to include, like this:

$text = strip_tags($textOrHtml, '<br /><br/><br><p>');


If you’re using a WYSIWYG Editor and are losing some formatting when using the MaxWord function, Jason Sauchuk of Interactive Tools suggests using this version of the function instead:

<?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;
}
?>

The first CMS Builder reference book is now available on-line!
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php