note 72019 deleted from function.strpos by danbrown

From: Date: Sun, 17 May 2009 16:33:27 +0000
Subject: note 72019 deleted from function.strpos by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154847@lists.php.net to get a copy of this message
Note Submitter: mvp at mvpprograms dot com ---- I understand the excitement of "admin at xylotspace dot com." I wrote three functions that I use in EVERY website I develop. What they do is get the text between strings. I made them case-insensitive (for php < 5) using "strtolower." This would not be necessary if you used "stripos." Now the first function is close to what "admin at xylotspace dot com" wrote, but does not have the position element. It also will return an empty string if no substring was found. If you want to get the title of an HTML document use: TextBetween('<title>','</title>',$content); The second function was revolutionary for me, because it gets an array of items between pairs of strings. So, with that I can grab most XML lists, or get all the links or images in a document. All the links in a document could be found using: TextBetweenArray('href="','"',$content); The third is less used, but is useful to process an array and get substrings within each record. //-----GET TEXT BETWEEN STRINGS------ function TextBetween($s1,$s2,$s){ $s1 = strtolower($s1); $s2 = strtolower($s2); $L1 = strlen($s1); $scheck = strtolower($s); if($L1>0){$pos1 = strpos($scheck,$s1);} else {$pos1=0;} if($pos1 !== false){ if($s2 == '') return substr($s,$pos1+$L1); $pos2 = strpos(substr($scheck,$pos1+$L1),$s2); if($pos2!==false) return substr($s,$pos1+$L1,$pos2); } return ''; } //-----GET ARRAY TEXT BETWEEN STRINGS------ function TextBetweenArray($s1,$s2,$s){ $myarray=array(); $s1=strtolower($s1); $s2=strtolower($s2); $L1=strlen($s1); $L2=strlen($s2); $scheck=strtolower($s); do{ $pos1 = strpos($scheck,$s1); if($pos1!==false){ $pos2 = strpos(substr($scheck,$pos1+$L1),$s2); if($pos2!==false){ $myarray[]=substr($s,$pos1+$L1,$pos2); $s=substr($s,$pos1+$L1+$pos2+$L2); $scheck=strtolower($s); } } } while (($pos1!==false)and($pos2!==false)); return $myarray; } //-----GET SUBTEXT IN ARRAY ITEMS------ function SubTextBetweenArray($s1,$s2,$myarray){ for ($i=0; $i< count($myarray); $i++) {$myarray[$i]=TextBetween($s1,$s2,$myarray[$i]);} return $myarray; }

« previous php.notes (#154847) next »