Recent Posts
- (09/10) Fixing Warning: the ECDSA host key for 'github.com' differs from the key for the IP addressTAGS:Web Server Admin
- (12/26) CakePHP 3 - Getting List of Column Definitions from a Table (like schema())TAGS:CakephpCake3
- (09/14) Change Order of Loaded Behaviors in CakePHP 3TAGS:Cake3CakephpWeb ProgrammingPhp
- (05/29) CakePHP 3 - Accept JSON Header Only Working When Debug FalseTAGS:Web ProgrammingCakephpCake3
- (05/23) Remove All Events from Google Calendar (Reset Calendar)TAGS:Web ProgrammingPhp
- (11/08) Google Tag Manager (GTM) Not Firing Default PageView EventTAGS:Web ProgrammingJavascriptGoogle Tag Manager
- (10/13) In PHP, how do you get __toString() Magic Method Result without calling echo?TAGS:CakephpCake3Cakephp 13PhpWeb Programming
- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
Subscribe to my feed
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,005 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.
- //in view.ctp
- uses('Flay');
- $Flay = new Flay();
- $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.
- //in view.ctp
- uses('Flay');
- $Flay = new Flay();
- $summary_html = $Flay->fragment($newsArticle['NewsArticle']['copy'], 150);
- $summary_html = str_replace(' ', '', $summary_html); //$Flay->toClean is having issues with turning   into a different charset in FF
- $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:
- uses('Flay');
- $Flay = new Flay()
- $summary_html = str_replace(' ', '', $newsArticle['NewsArticle']['copy']) //$Flay->toClean is having issues with turning   into a different charset
- $summary_html = $Flay->toClean($summary_html);
- $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.
- //in view.ctp
- uses('Flay');
- $Flay = new Flay();
- $string = "This is some sample text to try out.";
- $array_of_words = $Flay->extractWords($string);
- //output
- [0]=>
- string(4) "This"
- [1]=>
- string(2) "is"
- [2]=>
- string(4) "some"
- [3]=>
- string(6) "sample"
- [4]=>
- string(4) "text"
- [5]=>
- string(2) "to"
- [6]=>
- string(3) "try"
- [7]=>
- string(3) "out"
- [8]=>
- string(0) "" //period is turned into empty string
- }
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.
- //in view.ctp
- uses('Flay');
- $Flay = new Flay();
- $parsed_html = $Flay->toHtml($string); //parses string using flay syntax
- $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
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!