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

· 12년 전 · 2408 · 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,546
12년 전 조회 1,402
12년 전 조회 3,928
12년 전 조회 1,556
12년 전 조회 888
12년 전 조회 1,585
12년 전 조회 672
12년 전 조회 949
12년 전 조회 1,339
12년 전 조회 1,235
12년 전 조회 5,201
12년 전 조회 2,556
12년 전 조회 1,822
12년 전 조회 656
12년 전 조회 2,270
12년 전 조회 861
12년 전 조회 1,181
12년 전 조회 894
12년 전 조회 3,396
12년 전 조회 812
12년 전 조회 3,709
12년 전 조회 1,387
12년 전 조회 1,670
12년 전 조회 2,288
12년 전 조회 2,347
12년 전 조회 617
12년 전 조회 683
12년 전 조회 1,338
12년 전 조회 658
12년 전 조회 836
12년 전 조회 6,617
12년 전 조회 589
12년 전 조회 879
12년 전 조회 525
12년 전 조회 564
12년 전 조회 832
12년 전 조회 773
12년 전 조회 1,761
12년 전 조회 1,756
12년 전 조회 1,399
12년 전 조회 5,268
12년 전 조회 1,536
12년 전 조회 1,638
12년 전 조회 987
12년 전 조회 2,001
12년 전 조회 1,402
12년 전 조회 3,067
12년 전 조회 2,581
12년 전 조회 1,930
12년 전 조회 2,409
12년 전 조회 1,888
12년 전 조회 1,662
12년 전 조회 1,058
12년 전 조회 1,176
12년 전 조회 5,586
12년 전 조회 2,640
12년 전 조회 3,112
12년 전 조회 1,778
12년 전 조회 1,487
12년 전 조회 840
12년 전 조회 1,853
12년 전 조회 956
12년 전 조회 1,533
12년 전 조회 2,801
12년 전 조회 2,367
12년 전 조회 1,713
12년 전 조회 6,688
12년 전 조회 2,524
12년 전 조회 732
12년 전 조회 691
12년 전 조회 1,443
12년 전 조회 2,079
12년 전 조회 920
12년 전 조회 729
12년 전 조회 1,367
12년 전 조회 1,499
12년 전 조회 729
12년 전 조회 1,156
12년 전 조회 1,429
12년 전 조회 822
12년 전 조회 1,111
12년 전 조회 3,700
12년 전 조회 1,447
12년 전 조회 987
12년 전 조회 877
12년 전 조회 1,244
12년 전 조회 1,477
12년 전 조회 4,273
12년 전 조회 5,599
12년 전 조회 729
12년 전 조회 2,293
12년 전 조회 6,769
12년 전 조회 2,434
12년 전 조회 9,362
12년 전 조회 565
12년 전 조회 761
12년 전 조회 1,381
12년 전 조회 1,692
12년 전 조회 1,347
12년 전 조회 1,962
🐛 버그신고