note 25529 deleted from function.strpos by nlopess
| From: | nlopess@php.net | Date: | Tue, 25 Jul 2006 20:15:05 +0000 |
| Subject: | note 25529 deleted from function.strpos by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-115189@lists.php.net to get a copy of this message | ||
Note Submitter: admin at kingdompeople dot com
----
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..
<?php
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
<?php
$filetitle = stripfromtext ($content, "<P CLASS=title>", "</ P>");
// and
$rightmenuarray = stripfromtextarray($content, "<P CLASS=subtitle>", "</
P>");
foreach ($rightmenuarray as $rm) {$rightmenu.=$rm."<br>";}
?>