MainelyDesign.com Blog

Cakephp's Flay Class is Amazing - Examples Included

Posted on 09/08/2009 at 09:31 pm by Kevin Wentworth
Viewed 14,048 times | 0 comments

If you haven't checked out CakePHP's Flay class, check it out. It's one of the best utility classes for text output in CakePHP.

I just noticed that my blog was using the same META description for all of my blog entries- not good for SEO.  Instead, I want to pull a fragment of the article copy and use that as my META description.  I currently use this convoluted way to get my summaries from the article copy, so I decided to try using the Flay class.  I've used it before but I'm glad I revisited it... it's amazing!

Want to pull out just a fragment of a string?

Perfect for getting a summary of something, plus it adds an elipsis (...) at the end of the fragment.  Be careful, tags are kept intact.  I'm not sure what that means if you've opened a tag but haven't come to the closing tag.

  1. //in view.ctp
  2. uses('Flay');
  3. $Flay = new Flay();
  4. $summary_with_html = $Flay->fragment($newsArticle['NewsArticle']['copy'], 150);

Want to take that fragment and remove all HTML tags?

This is helpful if you want to preserve the format of your text, i.e. change <p></p> tags into line breaks.

  1. //in view.ctp
  2. uses('Flay');
  3. $Flay = new Flay();
  4. $summary_html = $Flay->fragment($newsArticle['NewsArticle']['copy'], 150);
  5. $summary_html = str_replace('&nbsp;', '', $summary_html);   //$Flay->toClean is having issues with turning &nbsp into a different charset in FF
  6. $summary = $Flay->toClean($summary_html);

Update: Use this method for an accurate fragment length:

I noticed the way I was excerpting my fragment above was wrong.  The 150 characters will include HTML (i.e. <a href="link.php">link</a>) in its calculation of the fragment length (instead of just the text "link").  To fix this, you need to "clean" the fragment of HTML first, then take the fragment.  Now a text fragment of 150 will excerpt 150 characters of words, not HTML instructions. Here's my updated fragment method:

  1. uses('Flay');
  2. $Flay = new Flay()
  3. $summary_html = str_replace('&nbsp;', '', $newsArticle['NewsArticle']['copy'])  //$Flay->toClean is having issues with turning &nbsp into a different charset
  4. $summary_html = $Flay->toClean($summary_html);
  5. $summary = $Flay->fragment($summary_html, 150); //I want a fragment 150 characters long

Want your string as an array of words instead?

You can use this method and all words will be returned in an array. Perfect for counting words, etc.

  1. //in view.ctp
  2. uses('Flay');
  3. $Flay = new Flay();
  4. $string = "This is some sample text to try out.";
  5. $array_of_words = $Flay->extractWords($string);
  6.  
  7. //output
  8. array(9) {
  9.   [0]=>
  10.   string(4) "This"
  11.   [1]=>
  12.   string(2) "is"
  13.   [2]=>
  14.   string(4) "some"
  15.   [3]=>
  16.   string(6) "sample"
  17.   [4]=>
  18.   string(4) "text"
  19.   [5]=>
  20.   string(2) "to"
  21.   [6]=>
  22.   string(3) "try"
  23.   [7]=>
  24.   string(3) "out"
  25.   [8]=>
  26.   string(0) ""  //period is turned into empty string
  27. }

Want to take a string and turn http:// into links?

Plus, you can do a lot more.  There is a whole syntax that you can use that will be parsed with the Flay class.

  1. //in view.ctp
  2. uses('Flay');
  3. $Flay = new Flay();
  4. $parsed_html = $Flay->toHtml($string);  //parses string using flay syntax
  5. $clean_parsed_html = $Flay->toParsedAndClean($string);  //will parse using Flay syntax and then clean out html tags

Isn't the Flay class amazing?  I can think of a thousand ways to use this in Site Avenger.

Cheers,

-Kevin Wentworth

Bookmark and Share

Tags for Cakephp's Flay Class is Amazing - Examples Included

Cakephp | Php | Seo | Site Avenger | Truncate | Usage | Web Programming

Comments for this Posting

No comments. Be the first to post a reply.

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design