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

· 12년 전 · 2395 · 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,528
12년 전 조회 1,379
12년 전 조회 3,912
12년 전 조회 1,531
12년 전 조회 869
12년 전 조회 1,562
12년 전 조회 657
12년 전 조회 931
12년 전 조회 1,324
12년 전 조회 1,211
12년 전 조회 5,180
12년 전 조회 2,539
12년 전 조회 1,798
12년 전 조회 644
12년 전 조회 2,249
12년 전 조회 843
12년 전 조회 1,156
12년 전 조회 873
12년 전 조회 3,382
12년 전 조회 794
12년 전 조회 3,685
12년 전 조회 1,374
12년 전 조회 1,656
12년 전 조회 2,271
12년 전 조회 2,333
12년 전 조회 593
12년 전 조회 668
12년 전 조회 1,324
12년 전 조회 639
12년 전 조회 816
12년 전 조회 6,600
12년 전 조회 574
12년 전 조회 865
12년 전 조회 504
12년 전 조회 545
12년 전 조회 812
12년 전 조회 751
12년 전 조회 1,750
12년 전 조회 1,740
12년 전 조회 1,380
12년 전 조회 5,249
12년 전 조회 1,522
12년 전 조회 1,623
12년 전 조회 976
12년 전 조회 1,990
12년 전 조회 1,391
12년 전 조회 3,054
12년 전 조회 2,562
12년 전 조회 1,918
12년 전 조회 2,396
12년 전 조회 1,875
12년 전 조회 1,647
12년 전 조회 1,044
12년 전 조회 1,156
12년 전 조회 5,574
12년 전 조회 2,620
12년 전 조회 3,103
12년 전 조회 1,757
12년 전 조회 1,473
12년 전 조회 825
12년 전 조회 1,834
12년 전 조회 940
12년 전 조회 1,518
12년 전 조회 2,783
12년 전 조회 2,354
12년 전 조회 1,696
12년 전 조회 6,675
12년 전 조회 2,504
12년 전 조회 725
12년 전 조회 678
12년 전 조회 1,428
12년 전 조회 2,073
12년 전 조회 911
12년 전 조회 721
12년 전 조회 1,353
12년 전 조회 1,488
12년 전 조회 715
12년 전 조회 1,141
12년 전 조회 1,413
12년 전 조회 804
12년 전 조회 1,093
12년 전 조회 3,686
12년 전 조회 1,436
12년 전 조회 976
12년 전 조회 864
12년 전 조회 1,228
12년 전 조회 1,462
12년 전 조회 4,250
12년 전 조회 5,586
12년 전 조회 716
12년 전 조회 2,283
12년 전 조회 6,747
12년 전 조회 2,421
12년 전 조회 9,344
12년 전 조회 549
12년 전 조회 750
12년 전 조회 1,367
12년 전 조회 1,683
12년 전 조회 1,337
12년 전 조회 1,948
🐛 버그신고