반응형
xmlOpen('http://blog.rss.naver.com/thinkfactory.xml','item');
$count = count($prt['item']);
for($x=0; $x<$count; $x++) {
echo $prt['title'][$x]['value'].'
';
echo $prt['link'][$x]['value'].'
';
echo $prt['description'][$x]['value'].'
';
}
-------------------------------------------------------------------------------------------
*/
function xmlOpen($url, $tag) {
$this->_tag = $tag;
if($fp = fopen($url, 'r')) {
while(!feof ($fp)) {
$xml_data .= fgets($fp, 4096);
}
fclose ($fp);
$this->_xmlDefine($xml_data);
return $this->_xmlInte();
} else {
$this->_error('xml open error : xml 파일열기 실패 => '.$url);
}
}
## xml 선언
function _xmlDefine($xml_data) {
preg_match('/encoding="[^"]+"/', $xml_data, $pattern);
$this->_xml_encoding = strtolower(preg_replace('/(encoding=)|(")/', '', $pattern[0]));
$this->_xml_parser = xml_parser_create();
xml_parser_set_option($this->_xml_parser, XML_OPTION_CASE_FOLDING, 0); //태그 이름을 소문자로 뿌려줌
xml_parse_into_struct($this->_xml_parser, $xml_data, $this->_xml_item, $index);
xml_parser_free($this->_xml_parser);
}
## xml 추출
function _xmlInte() {
foreach($this->_xml_item as $v) {
if($v['tag'] == $this->_tag && $v['type'] == 'open') {
$this->_xml_result[$v['tag']][] = '';
$this->_xml_chk = 'y';
}
if($v['type'] == 'complete' && $this->_xml_chk == 'y') {
if($this->_xml_encoding == 'utf-8') {
$this->_xml_result[$v['tag']][] = array('value'=>iconv('utf-8', 'euc-kr', $v['value']),'att'=>iconv('utf-8', 'euc-kr', $v['attributes']));
} else {
$this->_xml_result[$v['tag']][] = array('value'=>$v['value'],'att'=>$v['attributes']);
}
}
}
return $this->_xml_result;
}
## 에러표시
function _error($msg='') {
echo $msg;
exit;
}
}
?>
반응형
'Programming' 카테고리의 다른 글
| 자주사용하는 내장 함수 (0) | 2011.01.13 |
|---|---|
| 파일관련 PHP (0) | 2011.01.13 |
| 접속자 의 아이피(IP) 를 식별 (0) | 2011.01.11 |
| PHP 수학 함수 모음 (0) | 2011.01.11 |
| PHP 를 Shell script 로 사용 (0) | 2011.01.11 |