von Netbuster |
gut danke, nochmal |
von HoRnominatoR |
Netbuster schrieb am 10.09.2005 10:31
ich finde dort kein Beispiel!? |
oh ja, t'schuldigung, falsche seite
http://de.php.net/preg_match
da in den unteren kommentaren, hier der auszug:
email at albert-martin dot com
23-Oct-2004 11:39
Here is a faster way of extracting a special phrase from a HTML page:
Instead of using preg_match, e.g. like this:
preg_match("/<title>(.*)<\/title>/i", $html_content, $match);
use the following:
<?php
function ExtractString($str, $start, $end) {
$str_low = strtolower($str);
if (strpos($str_low, $start) !== false && strpos($str_lower, $end) !== false) {
$pos1 = strpos($str_low, $start) + strlen($start);
$pos2 = strpos($str_low, $end) - $pos1;
return substr($str, $pos1, $pos2);
}
}
$match = ExtractString($html_content, "<title>", "</title>");
?> |
|
von n0f3aR |
Überarbeitete Version:
1:
2:
3:
4: |
preg_match_all('#<title>(.+)</title>#', $html, $matches);
$title = ($matches[1][0]);
|
Du kannst alternativ aus dem Array $matches auch ALLE Titel auslesen, wenn es mehr als ein Treffer gibt ;) |
von Netbuster |
ich finde dort kein Beispiel!?
Ich hab zwar schon eins gefunden nur es klappt nicht:
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>";}
?> | |
von HoRnominatoR |
http://de2.php.net/strpos
einer der unteren kommentare. |
|