note 72612 deleted from function.strpos by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 16:33:12 +0000 |
| Subject: | note 72612 deleted from function.strpos by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154845@lists.php.net to get a copy of this message | ||
Note Submitter: IcEye
----
Get text between $s1 and $s2, return an array contains every occurrence (based on code of old
comment/s but with offset and without strtolower)
Sample:
$myDivsContent = getStrsBetween("<div","</div>",$myHtmlSrc);
Sample:
or...get rows for html table
...
...
//using TextBetween from old comment...
$aTable = TextBetween("<table","</table>",$myHtmlSrc);
$rows = getStrsBetween("<tr","</tr>",$aTable);
...
...
function getStrsBetween($s1,$s2,$s,$offset=0){
$result = array();
$index= 0;
$L1 = strlen($s1);
$found = false;
do{
if($L1>0){
$pos1 = strpos($s,$s1,$offset);
}
else {
$pos1=$offset;
}
if($pos1 !== false){
if($s2 == '')
$result[$index++]= substr($s,$pos1+$L1);
$pos2 = strpos(substr($s,$pos1+$L1),$s2,$L1);
if($pos2!==false){
$result[$index++]= substr($s,$pos1+$L1,$pos2);
$offset += $pos2 + strlen($s2);
}
else{
$pos1 = false;
}
}
}while($pos1 !== false);
return $result;
}
BUGs/Problems:
Function do not stop while $s1 is found in $s.