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

· 12년 전 · 2373 · 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,510
12년 전 조회 1,357
12년 전 조회 3,888
12년 전 조회 1,509
12년 전 조회 850
12년 전 조회 1,545
12년 전 조회 634
12년 전 조회 908
12년 전 조회 1,299
12년 전 조회 1,186
12년 전 조회 5,155
12년 전 조회 2,511
12년 전 조회 1,773
12년 전 조회 622
12년 전 조회 2,224
12년 전 조회 823
12년 전 조회 1,134
12년 전 조회 851
12년 전 조회 3,363
12년 전 조회 768
12년 전 조회 3,666
12년 전 조회 1,352
12년 전 조회 1,634
12년 전 조회 2,244
12년 전 조회 2,313
12년 전 조회 571
12년 전 조회 647
12년 전 조회 1,304
12년 전 조회 618
12년 전 조회 798
12년 전 조회 6,578
12년 전 조회 553
12년 전 조회 844
12년 전 조회 485
12년 전 조회 526
12년 전 조회 790
12년 전 조회 730
12년 전 조회 1,728
12년 전 조회 1,719
12년 전 조회 1,357
12년 전 조회 5,230
12년 전 조회 1,499
12년 전 조회 1,604
12년 전 조회 952
12년 전 조회 1,973
12년 전 조회 1,367
12년 전 조회 3,039
12년 전 조회 2,544
12년 전 조회 1,896
12년 전 조회 2,374
12년 전 조회 1,850
12년 전 조회 1,623
12년 전 조회 1,021
12년 전 조회 1,133
12년 전 조회 5,551
12년 전 조회 2,600
12년 전 조회 3,078
12년 전 조회 1,743
12년 전 조회 1,456
12년 전 조회 801
12년 전 조회 1,811
12년 전 조회 920
12년 전 조회 1,498
12년 전 조회 2,757
12년 전 조회 2,334
12년 전 조회 1,671
12년 전 조회 6,654
12년 전 조회 2,492
12년 전 조회 704
12년 전 조회 658
12년 전 조회 1,405
12년 전 조회 2,046
12년 전 조회 892
12년 전 조회 700
12년 전 조회 1,328
12년 전 조회 1,463
12년 전 조회 693
12년 전 조회 1,117
12년 전 조회 1,394
12년 전 조회 783
12년 전 조회 1,069
12년 전 조회 3,668
12년 전 조회 1,418
12년 전 조회 952
12년 전 조회 842
12년 전 조회 1,210
12년 전 조회 1,443
12년 전 조회 4,232
12년 전 조회 5,564
12년 전 조회 693
12년 전 조회 2,261
12년 전 조회 6,730
12년 전 조회 2,400
12년 전 조회 9,322
12년 전 조회 527
12년 전 조회 730
12년 전 조회 1,347
12년 전 조회 1,665
12년 전 조회 1,320
12년 전 조회 1,925
🐛 버그신고