note 25529 added to function.strpos

From: Date: Thu, 26 Sep 2002 18:19:10 +0000
Subject: note 25529 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-37298@lists.php.net to get a copy of this message
the following 2 functions grab a piece of content from an external html and lists that in separate places -- i.e. the title for html documents, or a list of subtopics dynamically.. the first function grabs the first found and returns a string, the second recursively searches the rest of the document and returns an array.. I found this was very useful to display articles on my website that had various topics marked by a consistent <P class=subtopic>, and the end </ P> tags.. function stripfromtext($haystack, $bfstarttext, $endsection) { $startpostext = $bfstarttext; $startposlen = strlen($startpostext); $startpos = strpos($haystack, $startpostext); $endpostext = $endsection; $endposlen = strlen($endpostext); $endpos = strpos($haystack, $endpostext, $startpos); return substr($haystack, $startpos + $startposlen, $endpos - ($startpos + $startposlen)); } function &stripfromtextarray($haystack, $bfstarttext, $endsection, $myarray=array(), $offset=0) { $startpostext = $bfstarttext; $startposlen = strlen($startpostext); $startpos = strpos($haystack, $startpostext, $offset); $endpostext = $endsection; $endposlen = strlen($endpostext); $endpos = strpos($haystack, $endpostext, $startpos); $myarray[] = substr($haystack, $startpos + $startposlen, $endpos - ($startpos + $startposlen)); $offset = $endpos; if (is_numeric(strpos($haystack, $startpostext, $offset))) { return stripfromtextarray($haystack,$startpostext, $endpostext, &$myarray, $offset); } else { return $myarray; } } The following is an example of the use of these functions -- $filetitle = stripfromtext ($content, "<P CLASS=title>", "</ P>"); and $rightmenuarray = stripfromtextarray($content, "<P CLASS=subtitle>", "</ P>"); foreach ($rightmenuarray as $rm) {$rightmenu.=$rm."<br>";} -- http://www.php.net/manual/en/function.strpos.php http://master.php.net/manage/user-notes.php?action=edit+25529 http://master.php.net/manage/user-notes.php?action=delete+25529 http://master.php.net/manage/user-notes.php?action=reject+25529

« previous php.notes (#37298) next »