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

· 12년 전 · 2394 · 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년 전 조회 1,527
12년 전 조회 1,378
12년 전 조회 3,911
12년 전 조회 1,531
12년 전 조회 867
12년 전 조회 1,561
12년 전 조회 656
12년 전 조회 930
12년 전 조회 1,321
12년 전 조회 1,209
12년 전 조회 5,180
12년 전 조회 2,538
12년 전 조회 1,797
12년 전 조회 644
12년 전 조회 2,246
12년 전 조회 842
12년 전 조회 1,155
12년 전 조회 873
12년 전 조회 3,379
12년 전 조회 793
12년 전 조회 3,685
12년 전 조회 1,373
12년 전 조회 1,655
12년 전 조회 2,268
12년 전 조회 2,331
12년 전 조회 589
12년 전 조회 666
12년 전 조회 1,323
12년 전 조회 638
12년 전 조회 816
12년 전 조회 6,599
12년 전 조회 574
12년 전 조회 865
12년 전 조회 503
12년 전 조회 544
12년 전 조회 810
12년 전 조회 750
12년 전 조회 1,748
12년 전 조회 1,740
12년 전 조회 1,380
12년 전 조회 5,249
12년 전 조회 1,521
12년 전 조회 1,621
12년 전 조회 973
12년 전 조회 1,989
12년 전 조회 1,389
12년 전 조회 3,050
12년 전 조회 2,561
12년 전 조회 1,917
12년 전 조회 2,395
12년 전 조회 1,874
12년 전 조회 1,645
12년 전 조회 1,043
12년 전 조회 1,155
12년 전 조회 5,574
12년 전 조회 2,620
12년 전 조회 3,103
12년 전 조회 1,755
12년 전 조회 1,473
12년 전 조회 824
12년 전 조회 1,833
12년 전 조회 939
12년 전 조회 1,518
12년 전 조회 2,782
12년 전 조회 2,351
12년 전 조회 1,694
12년 전 조회 6,675
12년 전 조회 2,503
12년 전 조회 724
12년 전 조회 678
12년 전 조회 1,426
12년 전 조회 2,073
12년 전 조회 910
12년 전 조회 720
12년 전 조회 1,352
12년 전 조회 1,486
12년 전 조회 714
12년 전 조회 1,138
12년 전 조회 1,413
12년 전 조회 803
12년 전 조회 1,092
12년 전 조회 3,685
12년 전 조회 1,436
12년 전 조회 975
12년 전 조회 864
12년 전 조회 1,227
12년 전 조회 1,461
12년 전 조회 4,250
12년 전 조회 5,585
12년 전 조회 715
12년 전 조회 2,282
12년 전 조회 6,747
12년 전 조회 2,420
12년 전 조회 9,342
12년 전 조회 549
12년 전 조회 749
12년 전 조회 1,366
12년 전 조회 1,681
12년 전 조회 1,337
12년 전 조회 1,947
🐛 버그신고