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

· 12년 전 · 3853 · 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년 전 조회 3,033
12년 전 조회 2,855
12년 전 조회 5,445
12년 전 조회 3,069
12년 전 조회 2,343
12년 전 조회 3,025
12년 전 조회 2,159
12년 전 조회 2,393
12년 전 조회 2,793
12년 전 조회 2,685
12년 전 조회 6,667
12년 전 조회 4,041
12년 전 조회 3,284
12년 전 조회 2,075
12년 전 조회 3,609
12년 전 조회 2,254
12년 전 조회 2,557
12년 전 조회 2,317
12년 전 조회 4,852
12년 전 조회 2,196
12년 전 조회 5,074
12년 전 조회 2,790
12년 전 조회 3,073
12년 전 조회 3,672
12년 전 조회 3,744
12년 전 조회 1,999
12년 전 조회 2,089
12년 전 조회 2,739
12년 전 조회 2,067
12년 전 조회 2,220
12년 전 조회 8,007
12년 전 조회 2,011
12년 전 조회 2,280
12년 전 조회 1,932
12년 전 조회 1,963
12년 전 조회 2,210
12년 전 조회 2,208
12년 전 조회 3,172
12년 전 조회 3,194
12년 전 조회 2,774
12년 전 조회 6,684
12년 전 조회 2,954
12년 전 조회 3,043
12년 전 조회 2,438
12년 전 조회 3,420
12년 전 조회 2,832
12년 전 조회 4,523
12년 전 조회 4,017
12년 전 조회 3,365
12년 전 조회 3,854
12년 전 조회 3,319
12년 전 조회 3,103
12년 전 조회 2,474
12년 전 조회 2,600
12년 전 조회 7,036
12년 전 조회 4,096
12년 전 조회 4,561
12년 전 조회 3,187
12년 전 조회 2,901
12년 전 조회 2,297
12년 전 조회 3,278
12년 전 조회 2,389
12년 전 조회 2,921
12년 전 조회 4,168
12년 전 조회 3,804
12년 전 조회 3,139
12년 전 조회 8,083
12년 전 조회 3,940
12년 전 조회 2,153
12년 전 조회 2,121
12년 전 조회 2,854
12년 전 조회 3,469
12년 전 조회 2,308
12년 전 조회 2,212
12년 전 조회 2,815
12년 전 조회 2,979
12년 전 조회 2,189
12년 전 조회 2,614
12년 전 조회 2,916
12년 전 조회 2,282
12년 전 조회 2,618
12년 전 조회 5,229
12년 전 조회 2,949
12년 전 조회 2,445
12년 전 조회 2,341
12년 전 조회 2,774
12년 전 조회 3,005
12년 전 조회 5,799
12년 전 조회 7,128
12년 전 조회 2,252
12년 전 조회 3,658
12년 전 조회 8,291
12년 전 조회 3,949
12년 전 조회 1.1만
12년 전 조회 2,067
12년 전 조회 2,238
12년 전 조회 2,928
12년 전 조회 3,233
12년 전 조회 2,866
12년 전 조회 3,349