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

· 12년 전 · 2399 · 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,539
12년 전 조회 1,396
12년 전 조회 3,923
12년 전 조회 1,549
12년 전 조회 880
12년 전 조회 1,577
12년 전 조회 663
12년 전 조회 943
12년 전 조회 1,336
12년 전 조회 1,227
12년 전 조회 5,197
12년 전 조회 2,550
12년 전 조회 1,815
12년 전 조회 650
12년 전 조회 2,263
12년 전 조회 858
12년 전 조회 1,175
12년 전 조회 890
12년 전 조회 3,391
12년 전 조회 811
12년 전 조회 3,700
12년 전 조회 1,385
12년 전 조회 1,665
12년 전 조회 2,284
12년 전 조회 2,344
12년 전 조회 609
12년 전 조회 680
12년 전 조회 1,335
12년 전 조회 651
12년 전 조회 831
12년 전 조회 6,611
12년 전 조회 583
12년 전 조회 872
12년 전 조회 518
12년 전 조회 560
12년 전 조회 826
12년 전 조회 764
12년 전 조회 1,758
12년 전 조회 1,749
12년 전 조회 1,394
12년 전 조회 5,263
12년 전 조회 1,532
12년 전 조회 1,630
12년 전 조회 985
12년 전 조회 2,001
12년 전 조회 1,399
12년 전 조회 3,066
12년 전 조회 2,580
12년 전 조회 1,928
12년 전 조회 2,400
12년 전 조회 1,885
12년 전 조회 1,658
12년 전 조회 1,054
12년 전 조회 1,167
12년 전 조회 5,584
12년 전 조회 2,633
12년 전 조회 3,110
12년 전 조회 1,771
12년 전 조회 1,483
12년 전 조회 833
12년 전 조회 1,850
12년 전 조회 952
12년 전 조회 1,528
12년 전 조회 2,797
12년 전 조회 2,360
12년 전 조회 1,709
12년 전 조회 6,685
12년 전 조회 2,520
12년 전 조회 730
12년 전 조회 686
12년 전 조회 1,437
12년 전 조회 2,078
12년 전 조회 917
12년 전 조회 727
12년 전 조회 1,365
12년 전 조회 1,498
12년 전 조회 726
12년 전 조회 1,151
12년 전 조회 1,424
12년 전 조회 812
12년 전 조회 1,107
12년 전 조회 3,696
12년 전 조회 1,444
12년 전 조회 983
12년 전 조회 875
12년 전 조회 1,241
12년 전 조회 1,475
12년 전 조회 4,266
12년 전 조회 5,596
12년 전 조회 725
12년 전 조회 2,291
12년 전 조회 6,760
12년 전 조회 2,432
12년 전 조회 9,354
12년 전 조회 559
12년 전 조회 757
12년 전 조회 1,375
12년 전 조회 1,690
12년 전 조회 1,343
12년 전 조회 1,957
🐛 버그신고