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

· 12년 전 · 2401 · 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,540
12년 전 조회 1,397
12년 전 조회 3,924
12년 전 조회 1,551
12년 전 조회 882
12년 전 조회 1,580
12년 전 조회 664
12년 전 조회 945
12년 전 조회 1,337
12년 전 조회 1,230
12년 전 조회 5,199
12년 전 조회 2,552
12년 전 조회 1,815
12년 전 조회 651
12년 전 조회 2,265
12년 전 조회 858
12년 전 조회 1,175
12년 전 조회 891
12년 전 조회 3,392
12년 전 조회 811
12년 전 조회 3,702
12년 전 조회 1,386
12년 전 조회 1,665
12년 전 조회 2,285
12년 전 조회 2,344
12년 전 조회 609
12년 전 조회 681
12년 전 조회 1,335
12년 전 조회 652
12년 전 조회 832
12년 전 조회 6,613
12년 전 조회 584
12년 전 조회 872
12년 전 조회 519
12년 전 조회 560
12년 전 조회 828
12년 전 조회 764
12년 전 조회 1,758
12년 전 조회 1,749
12년 전 조회 1,395
12년 전 조회 5,264
12년 전 조회 1,532
12년 전 조회 1,630
12년 전 조회 985
12년 전 조회 2,001
12년 전 조회 1,399
12년 전 조회 3,066
12년 전 조회 2,580
12년 전 조회 1,929
12년 전 조회 2,402
12년 전 조회 1,885
12년 전 조회 1,658
12년 전 조회 1,055
12년 전 조회 1,169
12년 전 조회 5,584
12년 전 조회 2,634
12년 전 조회 3,111
12년 전 조회 1,772
12년 전 조회 1,483
12년 전 조회 835
12년 전 조회 1,850
12년 전 조회 953
12년 전 조회 1,528
12년 전 조회 2,798
12년 전 조회 2,361
12년 전 조회 1,709
12년 전 조회 6,686
12년 전 조회 2,520
12년 전 조회 730
12년 전 조회 686
12년 전 조회 1,438
12년 전 조회 2,078
12년 전 조회 917
12년 전 조회 727
12년 전 조회 1,366
12년 전 조회 1,498
12년 전 조회 726
12년 전 조회 1,151
12년 전 조회 1,425
12년 전 조회 813
12년 전 조회 1,108
12년 전 조회 3,696
12년 전 조회 1,444
12년 전 조회 984
12년 전 조회 875
12년 전 조회 1,242
12년 전 조회 1,475
12년 전 조회 4,266
12년 전 조회 5,596
12년 전 조회 725
12년 전 조회 2,291
12년 전 조회 6,760
12년 전 조회 2,432
12년 전 조회 9,354
12년 전 조회 559
12년 전 조회 757
12년 전 조회 1,375
12년 전 조회 1,690
12년 전 조회 1,343
12년 전 조회 1,958
🐛 버그신고