WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 : 6322
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

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

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;
}
// }}}
?>



Print out array key => value in colored HTML
Categories : PHP, Arrays, HTML and PHP
Customizable encoding and decoding strings with security.
Categories : PHP, Strings, HTML and PHP
function textwrap will wrap text to any desired width using <BR>\n as the default line break. Default wrap width is 80 columns.
Categories : Strings, HTML and PHP, PHP
Form Submission Using Array's
Categories : PHP, HTML and PHP, Beginner Guides, Arrays
Tag content retrieval from websites with preg_match
Categories : PHP, Regexps, Arrays, HTML and PHP
string justification align center text
Categories : Strings, PHP, HTML and PHP
Parsing html tags with php. Get an array from this function
Categories : PHP, HTML and PHP, Arrays, Tag Extractors
Produces browser-safe strings while preserving HTML tags.
Categories : Strings, HTTP, PHP, HTML and PHP
PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use!
Categories : HTML and PHP, Email, PHP, Perl, HTML and PHP
Simple script to passing persistent and growing array between recalls of one page (manipulate little stack).
Categories : Arrays, Global Variables, PHP, HTML and PHP, Variables
How to ifconfig down/up a list of IP's
Categories : Arrays, Strings, Filesystem, PHP
Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, PHP
Text Wrapping
Categories : PHP, HTML and PHP, Strings
Function to create a separated list
Categories : PHP, Arrays, Strings
Grab images from one or more URLs and save them to a specified local directory.
Categories : PHP, Filesystem, Strings, Arrays