30 maj 2008

Wyświetlanie kodów ascii znaków

$__search = 'żądło';

$cnt = strlen($__search);
for($i=0; $i<$cnt; $i++) 
   echo $__search[$i].': '.ord($__search[$i]).' , '.dechex(ord($__search[$i])).' <br>';

Dla zmiennej wysłanej z formularza ze strony kodowanej w UTF-8 wygląda to tak:

ź 197, c5
?: 188, bc
ĺ 196, c4
?: 133, 85
d: 100, 64
ź 197, c5
?: 130, 82
o: 111, 6f

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;