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

· 12년 전 · 3027 · 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,174
12년 전 조회 2,036
12년 전 조회 4,713
12년 전 조회 2,342
12년 전 조회 1,481
12년 전 조회 2,192
12년 전 조회 1,302
12년 전 조회 1,556
12년 전 조회 1,956
12년 전 조회 1,866
12년 전 조회 5,800
12년 전 조회 3,172
12년 전 조회 2,436
12년 전 조회 1,251
12년 전 조회 2,798
12년 전 조회 1,433
12년 전 조회 1,757
12년 전 조회 1,489
12년 전 조회 4,002
12년 전 조회 1,351
12년 전 조회 4,265
12년 전 조회 1,984
12년 전 조회 2,246
12년 전 조회 2,848
12년 전 조회 2,937
12년 전 조회 1,190
12년 전 조회 1,251
12년 전 조회 1,920
12년 전 조회 1,239
12년 전 조회 1,431
12년 전 조회 7,175
12년 전 조회 1,163
12년 전 조회 1,485
12년 전 조회 1,113
12년 전 조회 1,145
12년 전 조회 1,391
12년 전 조회 1,362
12년 전 조회 2,332
12년 전 조회 2,356
12년 전 조회 1,973
12년 전 조회 5,860
12년 전 조회 2,127
12년 전 조회 2,231
12년 전 조회 1,584
12년 전 조회 2,599
12년 전 조회 1,986
12년 전 조회 3,661
12년 전 조회 3,152
12년 전 조회 2,551
12년 전 조회 3,028
12년 전 조회 2,473
12년 전 조회 2,285
12년 전 조회 1,657
12년 전 조회 1,783
12년 전 조회 6,162
12년 전 조회 3,259
12년 전 조회 3,739
12년 전 조회 2,356
12년 전 조회 2,067
12년 전 조회 1,429
12년 전 조회 2,434
12년 전 조회 1,556
12년 전 조회 2,128
12년 전 조회 3,352
12년 전 조회 2,986
12년 전 조회 2,294
12년 전 조회 7,228
12년 전 조회 3,056
12년 전 조회 1,327
12년 전 조회 1,269
12년 전 조회 2,005
12년 전 조회 2,630
12년 전 조회 1,485
12년 전 조회 1,331
12년 전 조회 1,945
12년 전 조회 2,108
12년 전 조회 1,290
12년 전 조회 1,729
12년 전 조회 2,027
12년 전 조회 1,412
12년 전 조회 1,723
12년 전 조회 4,349
12년 전 조회 2,055
12년 전 조회 1,568
12년 전 조회 1,465
12년 전 조회 1,841
12년 전 조회 2,108
12년 전 조회 4,863
12년 전 조회 6,213
12년 전 조회 1,328
12년 전 조회 3,066
12년 전 조회 7,355
12년 전 조회 3,010
12년 전 조회 1만
12년 전 조회 1,158
12년 전 조회 1,369
12년 전 조회 1,993
12년 전 조회 2,311
12년 전 조회 1,962
12년 전 조회 2,757