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

· 12년 전 · 3813 · 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,994
12년 전 조회 2,813
12년 전 조회 5,409
12년 전 조회 3,028
12년 전 조회 2,301
12년 전 조회 2,990
12년 전 조회 2,117
12년 전 조회 2,347
12년 전 조회 2,757
12년 전 조회 2,654
12년 전 조회 6,639
12년 전 조회 3,995
12년 전 조회 3,254
12년 전 조회 2,041
12년 전 조회 3,570
12년 전 조회 2,225
12년 전 조회 2,529
12년 전 조회 2,280
12년 전 조회 4,808
12년 전 조회 2,165
12년 전 조회 5,041
12년 전 조회 2,754
12년 전 조회 3,026
12년 전 조회 3,636
12년 전 조회 3,712
12년 전 조회 1,956
12년 전 조회 2,051
12년 전 조회 2,701
12년 전 조회 2,028
12년 전 조회 2,177
12년 전 조회 7,973
12년 전 조회 1,967
12년 전 조회 2,242
12년 전 조회 1,905
12년 전 조회 1,928
12년 전 조회 2,181
12년 전 조회 2,164
12년 전 조회 3,137
12년 전 조회 3,153
12년 전 조회 2,737
12년 전 조회 6,634
12년 전 조회 2,915
12년 전 조회 3,004
12년 전 조회 2,394
12년 전 조회 3,387
12년 전 조회 2,792
12년 전 조회 4,483
12년 전 조회 3,977
12년 전 조회 3,333
12년 전 조회 3,814
12년 전 조회 3,267
12년 전 조회 3,073
12년 전 조회 2,434
12년 전 조회 2,558
12년 전 조회 7,002
12년 전 조회 4,057
12년 전 조회 4,526
12년 전 조회 3,155
12년 전 조회 2,865
12년 전 조회 2,265
12년 전 조회 3,249
12년 전 조회 2,356
12년 전 조회 2,880
12년 전 조회 4,137
12년 전 조회 3,754
12년 전 조회 3,101
12년 전 조회 8,047
12년 전 조회 3,902
12년 전 조회 2,104
12년 전 조회 2,074
12년 전 조회 2,810
12년 전 조회 3,425
12년 전 조회 2,259
12년 전 조회 2,166
12년 전 조회 2,773
12년 전 조회 2,944
12년 전 조회 2,146
12년 전 조회 2,574
12년 전 조회 2,875
12년 전 조회 2,233
12년 전 조회 2,578
12년 전 조회 5,176
12년 전 조회 2,901
12년 전 조회 2,407
12년 전 조회 2,297
12년 전 조회 2,725
12년 전 조회 2,951
12년 전 조회 5,751
12년 전 조회 7,070
12년 전 조회 2,208
12년 전 조회 3,638
12년 전 조회 8,232
12년 전 조회 3,901
12년 전 조회 1.1만
12년 전 조회 2,020
12년 전 조회 2,199
12년 전 조회 2,882
12년 전 조회 3,187
12년 전 조회 2,822
12년 전 조회 3,333