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

· 12년 전 · 3869 · 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년 전 조회 3,048
12년 전 조회 2,870
12년 전 조회 5,461
12년 전 조회 3,079
12년 전 조회 2,354
12년 전 조회 3,042
12년 전 조회 2,179
12년 전 조회 2,418
12년 전 조회 2,804
12년 전 조회 2,695
12년 전 조회 6,683
12년 전 조회 4,050
12년 전 조회 3,302
12년 전 조회 2,090
12년 전 조회 3,633
12년 전 조회 2,267
12년 전 조회 2,569
12년 전 조회 2,335
12년 전 조회 4,869
12년 전 조회 2,216
12년 전 조회 5,086
12년 전 조회 2,812
12년 전 조회 3,089
12년 전 조회 3,690
12년 전 조회 3,757
12년 전 조회 2,010
12년 전 조회 2,104
12년 전 조회 2,750
12년 전 조회 2,083
12년 전 조회 2,236
12년 전 조회 8,025
12년 전 조회 2,026
12년 전 조회 2,292
12년 전 조회 1,945
12년 전 조회 1,976
12년 전 조회 2,232
12년 전 조회 2,219
12년 전 조회 3,193
12년 전 조회 3,211
12년 전 조회 2,792
12년 전 조회 6,701
12년 전 조회 2,971
12년 전 조회 3,062
12년 전 조회 2,449
12년 전 조회 3,433
12년 전 조회 2,849
12년 전 조회 4,536
12년 전 조회 4,036
12년 전 조회 3,381
12년 전 조회 3,870
12년 전 조회 3,331
12년 전 조회 3,118
12년 전 조회 2,489
12년 전 조회 2,611
12년 전 조회 7,048
12년 전 조회 4,110
12년 전 조회 4,580
12년 전 조회 3,200
12년 전 조회 2,911
12년 전 조회 2,310
12년 전 조회 3,294
12년 전 조회 2,406
12년 전 조회 2,939
12년 전 조회 4,184
12년 전 조회 3,823
12년 전 조회 3,156
12년 전 조회 8,101
12년 전 조회 3,951
12년 전 조회 2,172
12년 전 조회 2,145
12년 전 조회 2,871
12년 전 조회 3,492
12년 전 조회 2,329
12년 전 조회 2,230
12년 전 조회 2,837
12년 전 조회 3,004
12년 전 조회 2,198
12년 전 조회 2,632
12년 전 조회 2,946
12년 전 조회 2,307
12년 전 조회 2,647
12년 전 조회 5,256
12년 전 조회 2,969
12년 전 조회 2,461
12년 전 조회 2,363
12년 전 조회 2,792
12년 전 조회 3,029
12년 전 조회 5,829
12년 전 조회 7,144
12년 전 조회 2,277
12년 전 조회 3,665
12년 전 조회 8,307
12년 전 조회 3,974
12년 전 조회 1.1만
12년 전 조회 2,094
12년 전 조회 2,262
12년 전 조회 2,949
12년 전 조회 3,256
12년 전 조회 2,884
12년 전 조회 3,353