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

· 12년 전 · 2378 · 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,514
12년 전 조회 1,363
12년 전 조회 3,895
12년 전 조회 1,516
12년 전 조회 854
12년 전 조회 1,550
12년 전 조회 639
12년 전 조회 913
12년 전 조회 1,306
12년 전 조회 1,195
12년 전 조회 5,162
12년 전 조회 2,514
12년 전 조회 1,782
12년 전 조회 628
12년 전 조회 2,226
12년 전 조회 826
12년 전 조회 1,140
12년 전 조회 857
12년 전 조회 3,365
12년 전 조회 774
12년 전 조회 3,673
12년 전 조회 1,358
12년 전 조회 1,638
12년 전 조회 2,252
12년 전 조회 2,319
12년 전 조회 576
12년 전 조회 653
12년 전 조회 1,306
12년 전 조회 624
12년 전 조회 804
12년 전 조회 6,580
12년 전 조회 560
12년 전 조회 848
12년 전 조회 489
12년 전 조회 532
12년 전 조회 792
12년 전 조회 735
12년 전 조회 1,733
12년 전 조회 1,725
12년 전 조회 1,364
12년 전 조회 5,233
12년 전 조회 1,507
12년 전 조회 1,607
12년 전 조회 957
12년 전 조회 1,974
12년 전 조회 1,372
12년 전 조회 3,041
12년 전 조회 2,547
12년 전 조회 1,902
12년 전 조회 2,379
12년 전 조회 1,856
12년 전 조회 1,629
12년 전 조회 1,027
12년 전 조회 1,138
12년 전 조회 5,554
12년 전 조회 2,605
12년 전 조회 3,086
12년 전 조회 1,745
12년 전 조회 1,459
12년 전 조회 808
12년 전 조회 1,814
12년 전 조회 926
12년 전 조회 1,505
12년 전 조회 2,765
12년 전 조회 2,338
12년 전 조회 1,677
12년 전 조회 6,659
12년 전 조회 2,494
12년 전 조회 708
12년 전 조회 663
12년 전 조회 1,409
12년 전 조회 2,050
12년 전 조회 895
12년 전 조회 703
12년 전 조회 1,333
12년 전 조회 1,468
12년 전 조회 698
12년 전 조회 1,120
12년 전 조회 1,398
12년 전 조회 788
12년 전 조회 1,077
12년 전 조회 3,672
12년 전 조회 1,419
12년 전 조회 953
12년 전 조회 846
12년 전 조회 1,215
12년 전 조회 1,447
12년 전 조회 4,234
12년 전 조회 5,567
12년 전 조회 697
12년 전 조회 2,262
12년 전 조회 6,732
12년 전 조회 2,403
12년 전 조회 9,328
12년 전 조회 534
12년 전 조회 736
12년 전 조회 1,353
12년 전 조회 1,668
12년 전 조회 1,323
12년 전 조회 1,930
🐛 버그신고