환율 파싱
본문
네이버 환율 파싱 소스를 https://sir.kr/cm_free/1350194 에서 참고 하였습니다..
<?php
function Ncurrency() {
# 데이터 호출
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://finance.naver.com/marketindex/exchangeList.nhn');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$res = iconv('euc-kr', 'UTF-8', $response); if(!$response) return 'false';
# 파싱
preg_match("/<tbody.*?>.*?<\/[\s]*tbody>/s", $res, $tbody); if(!is_array($tbody)) return 'false';
preg_match_all('`<tr.*?>(.*?)<\/[\s]*tr>`s', $tbody[0], $tr); if(!is_array($tr)) return 'false';
$Data = array();
foreach($tr[0] as $k=>$v) {
unset($td, $akey);
preg_match_all('`<td.*?>(.*?)<\/td>`s', $v, $td);
$td = $td[0];
$akey = preg_replace('/([\xEA-\xED][\x80-\xBF]{2})+/', '', strip_tags($td[0]));
$akey = trim(str_replace('JPY (100)', 'JPY', $akey));
$akey = trim(str_replace('100', '', $akey)); if(!$akey) return 'false';
$Data[$akey]['통화명'] = trim(strip_tags($td[0]));
$Data[$akey]['매매기준율'] = str_replace(',', '', trim(strip_tags($td[1])));
$Data[$akey]['현찰살때'] = str_replace(',', '', trim(strip_tags($td[2])));
$Data[$akey]['현찰팔때'] = str_replace(',', '', trim(strip_tags($td[3])));
$Data[$akey]['송금보낼때'] = str_replace(',', '', trim(strip_tags($td[4])));
$Data[$akey]['송금받을때'] = str_replace(',', '', trim(strip_tags($td[5])));
$Data[$akey]['환가료율'] = str_replace(',', '', trim(strip_tags($td[6])));
$Data[$akey]['미화환산율'] = str_replace(',', '', trim(strip_tags($td[7])));
}
return $Data;
}
$Data = Ncurrency();
?>
-----------------------------------------------------------------------------------------------------------
<?php echo $Data['USD']['매매기준율']; ?> 이렇게 불러내었을때는 달러 기준으로 원화가 표시되는데
제가 필요한건 달러기준에서 페소(필리핀PHP)가 필요합니다.
어떻게 수정해야 할지 모르겠습니다.
고수님들 좀 번거럽겠지만 좀 자세히 가르쳐 주십시요..부탁드립니다.
답변 5
<?php echo $Data['PHP']['매매기준율']; ?>
달러에서 페소로 나타내려합니다.
1달러 = 1,135원, 1페소 = 21.30원 이면
1달러 = 1,135 / 21.30 하면 페소가 되는거지요 뭐 어렵나요?
1달러 = 53.29 페소 나오네요
$dollar = $Data['USD'][매매기준율] / $Data['PHP'][매매기준율];
생각해보세요
1달러가 1000원이고 1페소가 20원이면 1달러는 50페소 아닌가요?
초등 수학같은데...
그런제 저 소스로 파싱되나요?
감사 합니다. 제가 php를 잘 몰라서 ..