php변수를 스크립트로 사용하는 방법

· 12년 전 · 3672 · 2
<?php
// PHP변수 스크립트에서 쉽게 사용하기
function setScriptVar($name, $obj) {
global $script;
$script = "<script language='javascript'>";
if(is_object($obj)) {
$script .= "var $name = new Array();";
factory($name, $obj,'object');
} elseif(is_array($obj)) {
$script .= "var $name = new Array();";
factory($name, $obj,'array');
} else {
if(is_numeric($obj)) $script .= "var $name = $obj;";
else $script .= "var $name = '$obj';";
}
$script .= "</script>";
echo $script;
}
function factory($name, $data, $type = NULL) {
global $script;
foreach($data as $key => $val) {
if(is_object($val)){
factory($name.".".$key, $val, 'object');
} elseif(is_array($val)){
if($type == 'array') {
$script .= $name."[".$key."] = new Array();";
factory($name."[".$key."]", $val, 'array');
} else {
$script .= $name.".".$key." = new Array();";
factory($name.".".$key, $val, 'array');
}
} elseif(preg_match("/[a-zA-Z_][a-zA-Z0-9_]*/",$key) && $type == 'object') {
if(is_numeric($val)) $script .= $name.".".$key."=".$val.";";
else $script .= $name.".".$key."='".$val."';";
} elseif($type == 'array') {
if(!is_numeric($key)) $key = "'".$key."'";
if(is_numeric($val)) $script .= $name."[".$key."]=".$val.";";
else $script .= $name."[".$key."]='".$val."';";
}
}
}
// 사용법 setScriptVar(변수명지정,PHP변수);
// 일반변수
$market = "I Like Fruit!";
setScriptVar("market", $market);
// 배열
$fruit = array('Apple','Pineapple',array('Tomato','Banana'));
setScriptVar("fruit", $fruit);
// 오브젝트와 배열
$list->like = 'Pineapple';
$list->hate = 'Tomato';
$list->buy = array('Apple','Pineapple');
setScriptVar("list", $list);
// 변수타입
$num = array(100,'200');
setScriptVar("num",$num);
?>
<script>
//alert(market); // I Like Fruit!
//alert(fruit[0]+"/"+fruit[2][0]); // Apple/Tomato
//alert(list.like+"/"+list.buy[0]); // Pineapple/Apple
//alert(num[0] + 10); // 110
//alert(num[1] + 10); // 20010
</script>

첨부파일

php변수.txt (2 KB) 17회 2013-08-22 21:11
|

댓글 2개

감사해요 ㅎㅎ
유용한 정보네요
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
12년 전 조회 2,851
12년 전 조회 2,689
12년 전 조회 5,280
12년 전 조회 2,902
12년 전 조회 2,169
12년 전 조회 2,868
12년 전 조회 1,983
12년 전 조회 2,222
12년 전 조회 2,645
12년 전 조회 2,509
12년 전 조회 6,514
12년 전 조회 3,873
12년 전 조회 3,156
12년 전 조회 1,930
12년 전 조회 3,429
12년 전 조회 2,101
12년 전 조회 2,422
12년 전 조회 2,154
12년 전 조회 4,692
12년 전 조회 2,052
12년 전 조회 4,917
12년 전 조회 2,638
12년 전 조회 2,902
12년 전 조회 3,532
12년 전 조회 3,590
12년 전 조회 1,836
12년 전 조회 1,937
12년 전 조회 2,594
12년 전 조회 1,919
12년 전 조회 2,075
12년 전 조회 7,865
12년 전 조회 1,860
12년 전 조회 2,124
12년 전 조회 1,788
12년 전 조회 1,814
12년 전 조회 2,068
12년 전 조회 2,042
12년 전 조회 3,001
12년 전 조회 3,045
12년 전 조회 2,637
12년 전 조회 6,513
12년 전 조회 2,799
12년 전 조회 2,883
12년 전 조회 2,279
12년 전 조회 3,267
12년 전 조회 2,681
12년 전 조회 4,353
12년 전 조회 3,845
12년 전 조회 3,195
12년 전 조회 3,673
12년 전 조회 3,112
12년 전 조회 2,941
12년 전 조회 2,296
12년 전 조회 2,422
12년 전 조회 6,864
12년 전 조회 3,912
12년 전 조회 4,389
12년 전 조회 3,027
12년 전 조회 2,740
12년 전 조회 2,110
12년 전 조회 3,099
12년 전 조회 2,239
12년 전 조회 2,749
12년 전 조회 3,992
12년 전 조회 3,606
12년 전 조회 2,954
12년 전 조회 7,898
12년 전 조회 3,763
12년 전 조회 1,970
12년 전 조회 1,940
12년 전 조회 2,666
12년 전 조회 3,292
12년 전 조회 2,139
12년 전 조회 2,031
12년 전 조회 2,639
12년 전 조회 2,812
12년 전 조회 2,000
12년 전 조회 2,439
12년 전 조회 2,748
12년 전 조회 2,100
12년 전 조회 2,429
12년 전 조회 5,017
12년 전 조회 2,774
12년 전 조회 2,290
12년 전 조회 2,174
12년 전 조회 2,590
12년 전 조회 2,835
12년 전 조회 5,624
12년 전 조회 6,944
12년 전 조회 2,081
12년 전 조회 3,560
12년 전 조회 8,097
12년 전 조회 3,763
12년 전 조회 1.1만
12년 전 조회 1,900
12년 전 조회 2,077
12년 전 조회 2,777
12년 전 조회 3,080
12년 전 조회 2,702
12년 전 조회 3,249