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

· 12년 전 · 3716 · 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년 전 조회 2,886
12년 전 조회 2,719
12년 전 조회 5,305
12년 전 조회 2,939
12년 전 조회 2,197
12년 전 조회 2,898
12년 전 조회 2,007
12년 전 조회 2,254
12년 전 조회 2,665
12년 전 조회 2,549
12년 전 조회 6,549
12년 전 조회 3,906
12년 전 조회 3,179
12년 전 조회 1,962
12년 전 조회 3,474
12년 전 조회 2,126
12년 전 조회 2,451
12년 전 조회 2,187
12년 전 조회 4,721
12년 전 조회 2,081
12년 전 조회 4,953
12년 전 조회 2,676
12년 전 조회 2,932
12년 전 조회 3,557
12년 전 조회 3,628
12년 전 조회 1,876
12년 전 조회 1,971
12년 전 조회 2,627
12년 전 조회 1,952
12년 전 조회 2,105
12년 전 조회 7,893
12년 전 조회 1,895
12년 전 조회 2,158
12년 전 조회 1,827
12년 전 조회 1,852
12년 전 조회 2,108
12년 전 조회 2,081
12년 전 조회 3,042
12년 전 조회 3,079
12년 전 조회 2,665
12년 전 조회 6,550
12년 전 조회 2,830
12년 전 조회 2,917
12년 전 조회 2,304
12년 전 조회 3,301
12년 전 조회 2,716
12년 전 조회 4,390
12년 전 조회 3,896
12년 전 조회 3,238
12년 전 조회 3,717
12년 전 조회 3,155
12년 전 조회 2,986
12년 전 조회 2,331
12년 전 조회 2,457
12년 전 조회 6,905
12년 전 조회 3,964
12년 전 조회 4,433
12년 전 조회 3,068
12년 전 조회 2,781
12년 전 조회 2,161
12년 전 조회 3,144
12년 전 조회 2,274
12년 전 조회 2,789
12년 전 조회 4,048
12년 전 조회 3,651
12년 전 조회 2,999
12년 전 조회 7,938
12년 전 조회 3,806
12년 전 조회 2,014
12년 전 조회 1,979
12년 전 조회 2,708
12년 전 조회 3,342
12년 전 조회 2,171
12년 전 조회 2,066
12년 전 조회 2,677
12년 전 조회 2,846
12년 전 조회 2,043
12년 전 조회 2,481
12년 전 조회 2,775
12년 전 조회 2,137
12년 전 조회 2,476
12년 전 조회 5,065
12년 전 조회 2,802
12년 전 조회 2,326
12년 전 조회 2,212
12년 전 조회 2,626
12년 전 조회 2,873
12년 전 조회 5,654
12년 전 조회 6,980
12년 전 조회 2,113
12년 전 조회 3,582
12년 전 조회 8,132
12년 전 조회 3,798
12년 전 조회 1.1만
12년 전 조회 1,933
12년 전 조회 2,112
12년 전 조회 2,811
12년 전 조회 3,105
12년 전 조회 2,737
12년 전 조회 3,263