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

· 12년 전 · 2415 · 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,548
12년 전 조회 1,405
12년 전 조회 3,934
12년 전 조회 1,563
12년 전 조회 892
12년 전 조회 1,590
12년 전 조회 677
12년 전 조회 954
12년 전 조회 1,348
12년 전 조회 1,241
12년 전 조회 5,208
12년 전 조회 2,560
12년 전 조회 1,827
12년 전 조회 659
12년 전 조회 2,279
12년 전 조회 867
12년 전 조회 1,183
12년 전 조회 897
12년 전 조회 3,403
12년 전 조회 817
12년 전 조회 3,721
12년 전 조회 1,391
12년 전 조회 1,674
12년 전 조회 2,292
12년 전 조회 2,352
12년 전 조회 624
12년 전 조회 686
12년 전 조회 1,342
12년 전 조회 664
12년 전 조회 841
12년 전 조회 6,623
12년 전 조회 593
12년 전 조회 883
12년 전 조회 528
12년 전 조회 570
12년 전 조회 837
12년 전 조회 776
12년 전 조회 1,766
12년 전 조회 1,762
12년 전 조회 1,402
12년 전 조회 5,270
12년 전 조회 1,540
12년 전 조회 1,642
12년 전 조회 991
12년 전 조회 2,003
12년 전 조회 1,404
12년 전 조회 3,071
12년 전 조회 2,588
12년 전 조회 1,936
12년 전 조회 2,416
12년 전 조회 1,889
12년 전 조회 1,668
12년 전 조회 1,062
12년 전 조회 1,179
12년 전 조회 5,592
12년 전 조회 2,643
12년 전 조회 3,117
12년 전 조회 1,783
12년 전 조회 1,492
12년 전 조회 844
12년 전 조회 1,857
12년 전 조회 962
12년 전 조회 1,535
12년 전 조회 2,808
12년 전 조회 2,371
12년 전 조회 1,720
12년 전 조회 6,693
12년 전 조회 2,529
12년 전 조회 736
12년 전 조회 698
12년 전 조회 1,445
12년 전 조회 2,084
12년 전 조회 923
12년 전 조회 734
12년 전 조회 1,374
12년 전 조회 1,507
12년 전 조회 732
12년 전 조회 1,159
12년 전 조회 1,434
12년 전 조회 823
12년 전 조회 1,114
12년 전 조회 3,705
12년 전 조회 1,452
12년 전 조회 988
12년 전 조회 882
12년 전 조회 1,248
12년 전 조회 1,482
12년 전 조회 4,277
12년 전 조회 5,605
12년 전 조회 738
12년 전 조회 2,300
12년 전 조회 6,774
12년 전 조회 2,437
12년 전 조회 9,367
12년 전 조회 569
12년 전 조회 765
12년 전 조회 1,389
12년 전 조회 1,695
12년 전 조회 1,353
12년 전 조회 1,964
🐛 버그신고