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