25 lip 2008

Parsowanie dokumentu XML - przykład

$res = file_get_contents($url);
$content_start  = strpos($res, '</form>') + strlen('</form>');

$head_end       = $content_start;
$content_end    = strpos($res, '</xml-export>');
$foot_start     = strpos($res, '</xml-export>');

$head    = substr($res, 0, $head_end);
$content = substr($res, $content_start, $content_end - $content_start);
$foot    = substr($res, $foot_start);

$content = nl2br(html_entity_decode($content));
$content = htmlspecialchars_decode($content);
$content = UTF8toISO2($content);
$html_doc = $content;
2 kwi 2008

Wycięcie fragmentu tekstu zawartego miedzy tagami XML

Załadowanie XML:

$res = file_get_contents($url);

Wycięcie fragmentu tekstu:

$content_start  = strpos($res, '<content>') + strlen('<content>');
$head_end       = $content_start;
$content_end    = strpos($res, '</content>');
$foot_start     = strpos($res, '</content>');
$head    = substr($res, 0, $head_end);
$content = substr($res, $content_start, $content_end - $content_start);
$foot    = substr($res, $foot_start);
Konwersja nie zakodowanych znaków:
$content = str_replace("<",'<',$content);
$content = str_replace(">",'>',$content);
$res = $head . $content . $foot;