StringExtractComponent for CakePHP StringExtractComponent for CakePHP

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. I've also created two other examples with SQL substring between two characters SQL substring between two characters as well as a String Extract Extension Method with C#.

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 and it is important to truncate a string.

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 occurrence of the code syntax in this article. To make this a little prettier you can always use Custom Fonts using font-face with CSS. 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.

Published on Feb 28, 2009

Tags: CakePHP Tutorial | STREXTRACT | PHP | substring

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.


Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.