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

· 12년 전 · 2387 · 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,520
12년 전 조회 1,370
12년 전 조회 3,903
12년 전 조회 1,521
12년 전 조회 861
12년 전 조회 1,555
12년 전 조회 648
12년 전 조회 920
12년 전 조회 1,316
12년 전 조회 1,198
12년 전 조회 5,173
12년 전 조회 2,528
12년 전 조회 1,789
12년 전 조회 634
12년 전 조회 2,236
12년 전 조회 832
12년 전 조회 1,148
12년 전 조회 865
12년 전 조회 3,372
12년 전 조회 783
12년 전 조회 3,679
12년 전 조회 1,365
12년 전 조회 1,646
12년 전 조회 2,261
12년 전 조회 2,324
12년 전 조회 584
12년 전 조회 658
12년 전 조회 1,315
12년 전 조회 627
12년 전 조회 810
12년 전 조회 6,591
12년 전 조회 567
12년 전 조회 857
12년 전 조회 497
12년 전 조회 538
12년 전 조회 803
12년 전 조회 741
12년 전 조회 1,742
12년 전 조회 1,734
12년 전 조회 1,373
12년 전 조회 5,241
12년 전 조회 1,514
12년 전 조회 1,615
12년 전 조회 963
12년 전 조회 1,981
12년 전 조회 1,380
12년 전 조회 3,046
12년 전 조회 2,553
12년 전 조회 1,910
12년 전 조회 2,388
12년 전 조회 1,866
12년 전 조회 1,634
12년 전 조회 1,033
12년 전 조회 1,143
12년 전 조회 5,567
12년 전 조회 2,612
12년 전 조회 3,093
12년 전 조회 1,747
12년 전 조회 1,464
12년 전 조회 814
12년 전 조회 1,824
12년 전 조회 931
12년 전 조회 1,511
12년 전 조회 2,775
12년 전 조회 2,345
12년 전 조회 1,683
12년 전 조회 6,665
12년 전 조회 2,499
12년 전 조회 716
12년 전 조회 671
12년 전 조회 1,419
12년 전 조회 2,062
12년 전 조회 903
12년 전 조회 711
12년 전 조회 1,341
12년 전 조회 1,477
12년 전 조회 708
12년 전 조회 1,134
12년 전 조회 1,404
12년 전 조회 795
12년 전 조회 1,083
12년 전 조회 3,681
12년 전 조회 1,428
12년 전 조회 965
12년 전 조회 857
12년 전 조회 1,219
12년 전 조회 1,454
12년 전 조회 4,246
12년 전 조회 5,575
12년 전 조회 707
12년 전 조회 2,274
12년 전 조회 6,741
12년 전 조회 2,416
12년 전 조회 9,335
12년 전 조회 541
12년 전 조회 744
12년 전 조회 1,362
12년 전 조회 1,677
12년 전 조회 1,332
12년 전 조회 1,939
🐛 버그신고