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

· 12년 전 · 2393 · 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,527
12년 전 조회 1,378
12년 전 조회 3,910
12년 전 조회 1,530
12년 전 조회 867
12년 전 조회 1,561
12년 전 조회 655
12년 전 조회 929
12년 전 조회 1,320
12년 전 조회 1,207
12년 전 조회 5,179
12년 전 조회 2,537
12년 전 조회 1,797
12년 전 조회 643
12년 전 조회 2,246
12년 전 조회 841
12년 전 조회 1,154
12년 전 조회 873
12년 전 조회 3,378
12년 전 조회 793
12년 전 조회 3,685
12년 전 조회 1,373
12년 전 조회 1,655
12년 전 조회 2,268
12년 전 조회 2,330
12년 전 조회 588
12년 전 조회 666
12년 전 조회 1,323
12년 전 조회 637
12년 전 조회 816
12년 전 조회 6,599
12년 전 조회 574
12년 전 조회 864
12년 전 조회 503
12년 전 조회 544
12년 전 조회 809
12년 전 조회 749
12년 전 조회 1,748
12년 전 조회 1,739
12년 전 조회 1,380
12년 전 조회 5,249
12년 전 조회 1,521
12년 전 조회 1,621
12년 전 조회 971
12년 전 조회 1,987
12년 전 조회 1,389
12년 전 조회 3,049
12년 전 조회 2,560
12년 전 조회 1,917
12년 전 조회 2,394
12년 전 조회 1,873
12년 전 조회 1,644
12년 전 조회 1,042
12년 전 조회 1,153
12년 전 조회 5,573
12년 전 조회 2,620
12년 전 조회 3,102
12년 전 조회 1,753
12년 전 조회 1,471
12년 전 조회 824
12년 전 조회 1,831
12년 전 조회 938
12년 전 조회 1,517
12년 전 조회 2,782
12년 전 조회 2,349
12년 전 조회 1,691
12년 전 조회 6,675
12년 전 조회 2,501
12년 전 조회 724
12년 전 조회 677
12년 전 조회 1,426
12년 전 조회 2,072
12년 전 조회 908
12년 전 조회 719
12년 전 조회 1,352
12년 전 조회 1,486
12년 전 조회 713
12년 전 조회 1,138
12년 전 조회 1,412
12년 전 조회 801
12년 전 조회 1,091
12년 전 조회 3,684
12년 전 조회 1,435
12년 전 조회 975
12년 전 조회 864
12년 전 조회 1,227
12년 전 조회 1,461
12년 전 조회 4,250
12년 전 조회 5,585
12년 전 조회 715
12년 전 조회 2,282
12년 전 조회 6,746
12년 전 조회 2,420
12년 전 조회 9,341
12년 전 조회 549
12년 전 조회 749
12년 전 조회 1,366
12년 전 조회 1,681
12년 전 조회 1,337
12년 전 조회 1,947
🐛 버그신고