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

· 12년 전 · 2413 · 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,547
12년 전 조회 1,404
12년 전 조회 3,931
12년 전 조회 1,559
12년 전 조회 890
12년 전 조회 1,588
12년 전 조회 675
12년 전 조회 953
12년 전 조회 1,343
12년 전 조회 1,237
12년 전 조회 5,204
12년 전 조회 2,559
12년 전 조회 1,825
12년 전 조회 657
12년 전 조회 2,278
12년 전 조회 863
12년 전 조회 1,183
12년 전 조회 896
12년 전 조회 3,402
12년 전 조회 816
12년 전 조회 3,720
12년 전 조회 1,391
12년 전 조회 1,673
12년 전 조회 2,291
12년 전 조회 2,352
12년 전 조회 623
12년 전 조회 684
12년 전 조회 1,341
12년 전 조회 663
12년 전 조회 838
12년 전 조회 6,620
12년 전 조회 592
12년 전 조회 882
12년 전 조회 526
12년 전 조회 570
12년 전 조회 835
12년 전 조회 774
12년 전 조회 1,764
12년 전 조회 1,759
12년 전 조회 1,402
12년 전 조회 5,269
12년 전 조회 1,539
12년 전 조회 1,641
12년 전 조회 990
12년 전 조회 2,003
12년 전 조회 1,404
12년 전 조회 3,070
12년 전 조회 2,587
12년 전 조회 1,935
12년 전 조회 2,414
12년 전 조회 1,888
12년 전 조회 1,665
12년 전 조회 1,061
12년 전 조회 1,179
12년 전 조회 5,591
12년 전 조회 2,642
12년 전 조회 3,117
12년 전 조회 1,781
12년 전 조회 1,491
12년 전 조회 842
12년 전 조회 1,856
12년 전 조회 960
12년 전 조회 1,535
12년 전 조회 2,805
12년 전 조회 2,370
12년 전 조회 1,719
12년 전 조회 6,691
12년 전 조회 2,528
12년 전 조회 735
12년 전 조회 695
12년 전 조회 1,444
12년 전 조회 2,083
12년 전 조회 923
12년 전 조회 733
12년 전 조회 1,372
12년 전 조회 1,502
12년 전 조회 730
12년 전 조회 1,159
12년 전 조회 1,432
12년 전 조회 822
12년 전 조회 1,113
12년 전 조회 3,702
12년 전 조회 1,449
12년 전 조회 987
12년 전 조회 881
12년 전 조회 1,244
12년 전 조회 1,481
12년 전 조회 4,277
12년 전 조회 5,603
12년 전 조회 736
12년 전 조회 2,297
12년 전 조회 6,774
12년 전 조회 2,437
12년 전 조회 9,366
12년 전 조회 567
12년 전 조회 764
12년 전 조회 1,388
12년 전 조회 1,692
12년 전 조회 1,350
12년 전 조회 1,963
🐛 버그신고