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

· 12년 전 · 2398 · 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,537
12년 전 조회 1,393
12년 전 조회 3,923
12년 전 조회 1,548
12년 전 조회 877
12년 전 조회 1,577
12년 전 조회 662
12년 전 조회 940
12년 전 조회 1,332
12년 전 조회 1,224
12년 전 조회 5,192
12년 전 조회 2,548
12년 전 조회 1,813
12년 전 조회 650
12년 전 조회 2,260
12년 전 조회 855
12년 전 조회 1,175
12년 전 조회 887
12년 전 조회 3,391
12년 전 조회 810
12년 전 조회 3,698
12년 전 조회 1,384
12년 전 조회 1,665
12년 전 조회 2,281
12년 전 조회 2,342
12년 전 조회 606
12년 전 조회 679
12년 전 조회 1,335
12년 전 조회 651
12년 전 조회 828
12년 전 조회 6,609
12년 전 조회 581
12년 전 조회 870
12년 전 조회 516
12년 전 조회 558
12년 전 조회 822
12년 전 조회 763
12년 전 조회 1,757
12년 전 조회 1,747
12년 전 조회 1,390
12년 전 조회 5,261
12년 전 조회 1,531
12년 전 조회 1,628
12년 전 조회 983
12년 전 조회 1,999
12년 전 조회 1,396
12년 전 조회 3,066
12년 전 조회 2,578
12년 전 조회 1,926
12년 전 조회 2,399
12년 전 조회 1,881
12년 전 조회 1,658
12년 전 조회 1,052
12년 전 조회 1,166
12년 전 조회 5,584
12년 전 조회 2,631
12년 전 조회 3,109
12년 전 조회 1,771
12년 전 조회 1,483
12년 전 조회 832
12년 전 조회 1,844
12년 전 조회 949
12년 전 조회 1,526
12년 전 조회 2,795
12년 전 조회 2,359
12년 전 조회 1,707
12년 전 조회 6,684
12년 전 조회 2,519
12년 전 조회 729
12년 전 조회 685
12년 전 조회 1,433
12년 전 조회 2,078
12년 전 조회 916
12년 전 조회 726
12년 전 조회 1,362
12년 전 조회 1,496
12년 전 조회 724
12년 전 조회 1,151
12년 전 조회 1,423
12년 전 조회 811
12년 전 조회 1,106
12년 전 조회 3,695
12년 전 조회 1,442
12년 전 조회 983
12년 전 조회 875
12년 전 조회 1,240
12년 전 조회 1,474
12년 전 조회 4,264
12년 전 조회 5,595
12년 전 조회 722
12년 전 조회 2,290
12년 전 조회 6,760
12년 전 조회 2,431
12년 전 조회 9,353
12년 전 조회 558
12년 전 조회 756
12년 전 조회 1,369
12년 전 조회 1,690
12년 전 조회 1,341
12년 전 조회 1,956
🐛 버그신고