Follow @endyourif rss Facebook LinkedIn

StringExtractComponent for CakePHP StringExtractComponent for CakePHP

Published on Feb 28, 2009

Hi folks, I have just finished creating my first CakePHP component and I wanted to share it with the community.  After I finish writing this article, I will be submitting it to CakePHP for inclusion.

The component is called StringExtractComponent.  As you may guess, it extracts content from a piece of content.  I have created this component to mimic the STREXTRACT function in Microsoft Visual FoxPro.  I have come to fall in love with this function because it makes retrieving data from a string with a start and end delimiter.

To help you understand how it is used, I read the contents of one of my articles into a string.  I then proceed to extract a specific section that is contained within the code syntaxes that exist in the article.

The StringExtract Component:

* StringExtract Component
* In Visual FoxPro there is an excellent function called STREXTRACT
* that allows you to pass in a start and optional end delimiter
* and it returns the text between the start and end delimiter.
* This is a PHP version of the same function to save time having
* to find and substring it each time.
* @copyright    Copyright 2009, Jamie Munro
* @link
* @author       Jamie Munro
* @version      1.0
* @license      MIT
class StringExtractComponent extends Object {
 * str_extract parses out and returns the text specified between start and end delimiters
 * @param string $content String containing the content to parse
 * @param string $begindelim String containing the text of where to start parsing
 * @param string $enddelim String containing the text of where to end parsing
 * @param int $occur Integer defining which instance of the $begin parameter
 * @return string
 * @access public
 function str_extract($content, $begindelim, $enddelim = "", $occur = 0) {
  $parsedContent = "";
  // don't bother doing any work if content is empty
  if (strlen($content)) {
   $count = 0;
   $start = 0;
   // start a loop for the occurance
   do {
    // get the starting position of the content
    $start = strpos($content, $begindelim, $start);
    // don't bother doing any more if start was not found
    if ($start === false) {
    } else {
     // since we found it, we want to add the length
     // of the begin delimiter to it so it doesn't get
     // included when we parse the string
     $start += strlen($begindelim);
   } while ($count <= $occur);
   // if start is false, we didn't find it, so we should not parse anything
   if ($start !== false) {
    // if $end is nothing, set the end of the parsing to the length of the content
    $end = (bool)false;
    if (strlen($enddelim)) {
     // find the end delimiter
     $end = strpos($content, $enddelim, $start);
    // if enddelim was not found or not provided set the end to the length of the content
    if ($end === false) {
     $end = strlen($content);
    // now we have the start and end, parse it out
    $parsedContent = substr($content, $start, $end - $start);
  return $parsedContent;

Here is an example of how to use the component:

class TestsController extends AppController {

 var $uses = array();
 var $components = array('StringExtract');

 function index() {
  $content = file_get_contents('');
  $start = '<pre class="php" style="font-family:monospace;">';
  $end = '</pre>';
  $output = $this->StringExtract->str_extract($content, $start, $end, 3);
  echo $output;
The above code stores the results of my database indexing article into the variable $content.  I then specify the start and end delimiter of where the content I want returned is.  I also pass in 3, indicating that I want the 4th occurance of the code synax in this article.  If the TestsController is run it returns the following:

<li style="font-weight: normal; vertical-align:top;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">ALTER TABLE `users` ADD INDEX ( `email` )</div>
That's it, I hope you find this component as useful as I have.

Tags: CakePHP Tutorial | php | STREXTRACT | Visual FoxPro | PHP

My Books
ASP.NET MVC 5 With Bootstrap and Knockout.js
Knockout.js Building Dynamic Client-Side Applications
20 Recipes for Programming MVC 3
20 Recipes for Programming PhoneGap
Rapid Application Development with CakePHP