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

· 12년 전 · 3116 · 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,273
12년 전 조회 2,138
12년 전 조회 4,828
12년 전 조회 2,445
12년 전 조회 1,578
12년 전 조회 2,292
12년 전 조회 1,406
12년 전 조회 1,662
12년 전 조회 2,063
12년 전 조회 1,962
12년 전 조회 5,886
12년 전 조회 3,267
12년 전 조회 2,527
12년 전 조회 1,334
12년 전 조회 2,885
12년 전 조회 1,516
12년 전 조회 1,842
12년 전 조회 1,566
12년 전 조회 4,068
12년 전 조회 1,434
12년 전 조회 4,335
12년 전 조회 2,052
12년 전 조회 2,314
12년 전 조회 2,914
12년 전 조회 2,985
12년 전 조회 1,265
12년 전 조회 1,320
12년 전 조회 1,982
12년 전 조회 1,308
12년 전 조회 1,493
12년 전 조회 7,249
12년 전 조회 1,240
12년 전 조회 1,556
12년 전 조회 1,189
12년 전 조회 1,223
12년 전 조회 1,467
12년 전 조회 1,427
12년 전 조회 2,412
12년 전 조회 2,425
12년 전 조회 2,044
12년 전 조회 5,924
12년 전 조회 2,203
12년 전 조회 2,304
12년 전 조회 1,665
12년 전 조회 2,679
12년 전 조회 2,068
12년 전 조회 3,740
12년 전 조회 3,245
12년 전 조회 2,643
12년 전 조회 3,117
12년 전 조회 2,569
12년 전 조회 2,375
12년 전 조회 1,734
12년 전 조회 1,876
12년 전 조회 6,254
12년 전 조회 3,343
12년 전 조회 3,831
12년 전 조회 2,440
12년 전 조회 2,138
12년 전 조회 1,516
12년 전 조회 2,511
12년 전 조회 1,636
12년 전 조회 2,223
12년 전 조회 3,434
12년 전 조회 3,066
12년 전 조회 2,384
12년 전 조회 7,320
12년 전 조회 3,134
12년 전 조회 1,413
12년 전 조회 1,355
12년 전 조회 2,093
12년 전 조회 2,722
12년 전 조회 1,579
12년 전 조회 1,426
12년 전 조회 2,049
12년 전 조회 2,205
12년 전 조회 1,391
12년 전 조회 1,826
12년 전 조회 2,134
12년 전 조회 1,508
12년 전 조회 1,835
12년 전 조회 4,460
12년 전 조회 2,159
12년 전 조회 1,675
12년 전 조회 1,577
12년 전 조회 1,953
12년 전 조회 2,206
12년 전 조회 4,976
12년 전 조회 6,314
12년 전 조회 1,432
12년 전 조회 3,170
12년 전 조회 7,455
12년 전 조회 3,126
12년 전 조회 1만
12년 전 조회 1,259
12년 전 조회 1,469
12년 전 조회 2,100
12년 전 조회 2,415
12년 전 조회 2,064
12년 전 조회 2,856