note 72019 added to function.strpos

From: Date: Fri, 29 Dec 2006 08:30:51 +0000
Subject: note 72019 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-121001@lists.php.net to get a copy of this message
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; } ---- Server IP: 64.71.164.2 Probable Submitter: 24.20.177.246 ---- Manual Page -- http://www.php.net/manual/en/function.strpos.php Edit -- https://master.php.net/note/edit/72019 Del: integrated -- https://master.php.net/note/delete/72019/integrated Del: useless -- https://master.php.net/note/delete/72019/useless Del: bad code -- https://master.php.net/note/delete/72019/bad+code Del: spam -- https://master.php.net/note/delete/72019/spam Del: non-english -- https://master.php.net/note/delete/72019/non-english Del: in docs -- https://master.php.net/note/delete/72019/in+docs Del: other reasons-- https://master.php.net/note/delete/72019 Reject -- https://master.php.net/note/reject/72019 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#121001) next »