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

· 12년 전 · 2432 · 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,560
12년 전 조회 1,425
12년 전 조회 3,954
12년 전 조회 1,581
12년 전 조회 915
12년 전 조회 1,608
12년 전 조회 694
12년 전 조회 972
12년 전 조회 1,360
12년 전 조회 1,264
12년 전 조회 5,224
12년 전 조회 2,577
12년 전 조회 1,845
12년 전 조회 681
12년 전 조회 2,293
12년 전 조회 889
12년 전 조회 1,201
12년 전 조회 916
12년 전 조회 3,426
12년 전 조회 834
12년 전 조회 3,737
12년 전 조회 1,406
12년 전 조회 1,697
12년 전 조회 2,309
12년 전 조회 2,375
12년 전 조회 640
12년 전 조회 708
12년 전 조회 1,368
12년 전 조회 682
12년 전 조회 862
12년 전 조회 6,634
12년 전 조회 613
12년 전 조회 898
12년 전 조회 548
12년 전 조회 583
12년 전 조회 857
12년 전 조회 792
12년 전 조회 1,795
12년 전 조회 1,780
12년 전 조회 1,413
12년 전 조회 5,290
12년 전 조회 1,565
12년 전 조회 1,660
12년 전 조회 1,009
12년 전 조회 2,022
12년 전 조회 1,426
12년 전 조회 3,096
12년 전 조회 2,610
12년 전 조회 1,954
12년 전 조회 2,433
12년 전 조회 1,901
12년 전 조회 1,677
12년 전 조회 1,080
12년 전 조회 1,193
12년 전 조회 5,609
12년 전 조회 2,665
12년 전 조회 3,134
12년 전 조회 1,791
12년 전 조회 1,513
12년 전 조회 864
12년 전 조회 1,880
12년 전 조회 978
12년 전 조회 1,556
12년 전 조회 2,820
12년 전 조회 2,386
12년 전 조회 1,731
12년 전 조회 6,713
12년 전 조회 2,544
12년 전 조회 755
12년 전 조회 712
12년 전 조회 1,459
12년 전 조회 2,111
12년 전 조회 935
12년 전 조회 754
12년 전 조회 1,390
12년 전 조회 1,527
12년 전 조회 751
12년 전 조회 1,178
12년 전 조회 1,448
12년 전 조회 844
12년 전 조회 1,130
12년 전 조회 3,720
12년 전 조회 1,474
12년 전 조회 1,002
12년 전 조회 898
12년 전 조회 1,267
12년 전 조회 1,502
12년 전 조회 4,293
12년 전 조회 5,620
12년 전 조회 755
12년 전 조회 2,311
12년 전 조회 6,792
12년 전 조회 2,456
12년 전 조회 9,388
12년 전 조회 581
12년 전 조회 782
12년 전 조회 1,410
12년 전 조회 1,713
12년 전 조회 1,374
12년 전 조회 1,978
🐛 버그신고