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

· 12년 전 · 2380 · 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,514
12년 전 조회 1,363
12년 전 조회 3,896
12년 전 조회 1,516
12년 전 조회 855
12년 전 조회 1,550
12년 전 조회 640
12년 전 조회 914
12년 전 조회 1,307
12년 전 조회 1,195
12년 전 조회 5,162
12년 전 조회 2,517
12년 전 조회 1,783
12년 전 조회 629
12년 전 조회 2,226
12년 전 조회 826
12년 전 조회 1,141
12년 전 조회 857
12년 전 조회 3,365
12년 전 조회 774
12년 전 조회 3,674
12년 전 조회 1,359
12년 전 조회 1,639
12년 전 조회 2,252
12년 전 조회 2,320
12년 전 조회 576
12년 전 조회 653
12년 전 조회 1,308
12년 전 조회 624
12년 전 조회 804
12년 전 조회 6,580
12년 전 조회 561
12년 전 조회 849
12년 전 조회 490
12년 전 조회 532
12년 전 조회 792
12년 전 조회 735
12년 전 조회 1,736
12년 전 조회 1,726
12년 전 조회 1,364
12년 전 조회 5,233
12년 전 조회 1,508
12년 전 조회 1,607
12년 전 조회 959
12년 전 조회 1,976
12년 전 조회 1,373
12년 전 조회 3,041
12년 전 조회 2,547
12년 전 조회 1,903
12년 전 조회 2,381
12년 전 조회 1,856
12년 전 조회 1,630
12년 전 조회 1,028
12년 전 조회 1,139
12년 전 조회 5,555
12년 전 조회 2,606
12년 전 조회 3,087
12년 전 조회 1,745
12년 전 조회 1,459
12년 전 조회 808
12년 전 조회 1,815
12년 전 조회 926
12년 전 조회 1,505
12년 전 조회 2,765
12년 전 조회 2,338
12년 전 조회 1,678
12년 전 조회 6,659
12년 전 조회 2,495
12년 전 조회 708
12년 전 조회 664
12년 전 조회 1,409
12년 전 조회 2,052
12년 전 조회 897
12년 전 조회 705
12년 전 조회 1,334
12년 전 조회 1,469
12년 전 조회 698
12년 전 조회 1,120
12년 전 조회 1,398
12년 전 조회 788
12년 전 조회 1,077
12년 전 조회 3,672
12년 전 조회 1,419
12년 전 조회 953
12년 전 조회 846
12년 전 조회 1,216
12년 전 조회 1,448
12년 전 조회 4,237
12년 전 조회 5,568
12년 전 조회 698
12년 전 조회 2,263
12년 전 조회 6,732
12년 전 조회 2,406
12년 전 조회 9,329
12년 전 조회 534
12년 전 조회 736
12년 전 조회 1,354
12년 전 조회 1,670
12년 전 조회 1,324
12년 전 조회 1,930
🐛 버그신고