WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Parse string to find sub-string between two arbitrary strings
Categories : PHP, Strings, HTML and PHP, Arrays Click here to Update Your Picture
Chris Allison
Date : Apr 21st 2009
Grade : 2 of 5 (graded 4 times)
Viewed : 3079
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Chris Allison
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

Function to retrieve the contents of a string between two string delimiters. Was written to help parse HTML content, but can be used to parse any string with any start and end strings.

<?php
function textBetween($subject="",$start="",$end="") // {{{
{
   
/*
        * function to find contents of a string between two arbitrary sub-strings
        *
        * it handles nested delimiters
        * it will return up to the end of $subject if $start is found and $end is not found
        *
        * input:
        * $subject is the string to search in
        * $start is the sub-string to search for as the beginning of the required contents
        * $end is the sub-string to search for as the end of the required contents
        *
        * output:
        * array:
        *   "start" => the starting position in the subject of the $start sub-string
        *   "end" => the ending position in the subject of the $end sub-string
        *   "text" => the contents of $subject between the 2 delimiters, or $subject if
        *             $start delimiter is not found
        *   "between" => the contents of $subject between the 2 delimiters, or an empty
        *                string if $start is not found
        *   "left" => the left part of $subject before $start was found
        *   "right" => the right part of $subject after $end was found
        *   "outside" => the concatenated contents of "left" and "right"
     */
   
$ret=array("start"=>false,"end"=>false,"text"=>$subject,"between"=>"","left"=>"","right"=>"");
    if(
is_string($ret["text"]) && is_string($start)){
       
$subl=strlen($ret["text"]);
       
$startl=strlen($start);
        if(
$subl && $startl){
           
/* find the beginning delimiter */
           
$ret["start"]=strpos($ret["text"],$start);
            if(
$ret["start"]!==false){
               
/* starting delimiter found */
               
$offset=$ret["start"]+$startl;
               
$ret["text"]=substr($ret["text"],$offset);
                if(
is_string($end)){
                   
$endl=strlen($end);
                    if(
$endl){
                       
$ret["end"]=strpos($ret["text"],$end);
                        if(
$ret["end"]!==false){
                           
$checknested=true;
                           
$tmppos=0;
                            while(
$checknested){
                               
/* check for nested delimiters
                                    search between the start point
                                    and the currently found end point */
                               
$ttp=strpos(substr($ret["text"],$tmppos,$ret["end"]-$tmppos),$start);
                                if(
$ttp!==false){
                                   
$tmppos+=$ttp;
                                   
/* we have nested delimiters
                                        move the end point onto the next
                                        delimiter */
                                   
$ret["end"]=strpos($ret["text"],$end,$ret["end"]+1);
                                    if(
$ret["end"]!==false){
                                       
$tmppos++;
                                    }else{
                                       
$checknested=false;
                                    }
                                }else{
                                   
/* there are no nested delimiters */
                                   
$checknested=false;
                                    if(
$ret["end"]!==false){
                                       
$ret["text"]=substr($ret["text"],0,$ret["end"]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if(
$ret["text"]!=$subject){
       
$ret["between"]=$ret["text"];
    }
    if(
$ret["start"]!==false){
        if(
$ret["start"]>0){
           
$ret["outside"]=substr($subject,0,$ret["start"]);
           
$ret["left"]=$ret["outside"];
        }
        if(
$ret["end"]!==false && $ret["end"]>0){
           
$ret["right"]=substr($subject,$ret["start"]+$ret["end"]+$startl+$endl);
           
$ret["outside"].=$ret["right"];
        }
    }else{
       
$ret["outside"]=$subject;
    }
   
// $ret["cc"]=$cc;
   
return $ret;
}
// }}}
?>



Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, PHP
WWW interface to Unix Manual(phpMan)
Categories : Program Execution, Strings, Arrays, PHP
Print out array key => value in colored HTML
Categories : PHP, Arrays, HTML and PHP
Get TemplateMonster data
Categories : Arrays, Ecommerce, PHP, Strings
Form Submission Using Array's
Categories : PHP, HTML and PHP, Beginner Guides, Arrays
Can the word DO be used in arrays?
Categories : Arrays, PHP, Strings
Customizable encoding and decoding strings with security.
Categories : PHP, Strings, HTML and PHP
navbar.php3 - Dynamic hyperlinked navigation bars
Categories : HTML and PHP, Arrays, PHP, Complete Programs
Parsing html tags with php. Get an array from this function
Categories : PHP, HTML and PHP, Arrays, Tag Extractors
Grab images from one or more URLs and save them to a specified local directory.
Categories : PHP, Filesystem, Strings, Arrays
How to Get a character array from a string
Categories : PHP, Strings, Arrays
Takes an array and returns a string, suitable for inputing in an SQL statement
Categories : Arrays, Strings, PHP
Pull deliniated text strings into a "SELECT" statement in a form.
Categories : HTML and PHP, PHP, Strings
columned txt file to array()?
Categories : Arrays, Strings, Regexps, PHP
Compare two texts and display a block of text with the differences between them.
Categories : PHP, PHP Classes, Filesystem, Strings, Arrays