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

· 12년 전 · 2371 · 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,507
12년 전 조회 1,356
12년 전 조회 3,887
12년 전 조회 1,509
12년 전 조회 846
12년 전 조회 1,544
12년 전 조회 633
12년 전 조회 905
12년 전 조회 1,299
12년 전 조회 1,182
12년 전 조회 5,154
12년 전 조회 2,511
12년 전 조회 1,773
12년 전 조회 620
12년 전 조회 2,224
12년 전 조회 820
12년 전 조회 1,133
12년 전 조회 848
12년 전 조회 3,362
12년 전 조회 767
12년 전 조회 3,665
12년 전 조회 1,349
12년 전 조회 1,632
12년 전 조회 2,243
12년 전 조회 2,310
12년 전 조회 568
12년 전 조회 644
12년 전 조회 1,300
12년 전 조회 616
12년 전 조회 797
12년 전 조회 6,578
12년 전 조회 551
12년 전 조회 843
12년 전 조회 482
12년 전 조회 522
12년 전 조회 790
12년 전 조회 727
12년 전 조회 1,727
12년 전 조회 1,716
12년 전 조회 1,355
12년 전 조회 5,227
12년 전 조회 1,498
12년 전 조회 1,600
12년 전 조회 952
12년 전 조회 1,971
12년 전 조회 1,366
12년 전 조회 3,038
12년 전 조회 2,543
12년 전 조회 1,894
12년 전 조회 2,372
12년 전 조회 1,849
12년 전 조회 1,623
12년 전 조회 1,019
12년 전 조회 1,129
12년 전 조회 5,551
12년 전 조회 2,595
12년 전 조회 3,076
12년 전 조회 1,740
12년 전 조회 1,455
12년 전 조회 799
12년 전 조회 1,806
12년 전 조회 917
12년 전 조회 1,496
12년 전 조회 2,754
12년 전 조회 2,328
12년 전 조회 1,670
12년 전 조회 6,654
12년 전 조회 2,490
12년 전 조회 700
12년 전 조회 658
12년 전 조회 1,405
12년 전 조회 2,046
12년 전 조회 887
12년 전 조회 698
12년 전 조회 1,328
12년 전 조회 1,461
12년 전 조회 691
12년 전 조회 1,116
12년 전 조회 1,392
12년 전 조회 782
12년 전 조회 1,068
12년 전 조회 3,665
12년 전 조회 1,416
12년 전 조회 951
12년 전 조회 838
12년 전 조회 1,208
12년 전 조회 1,442
12년 전 조회 4,232
12년 전 조회 5,564
12년 전 조회 689
12년 전 조회 2,261
12년 전 조회 6,727
12년 전 조회 2,399
12년 전 조회 9,321
12년 전 조회 523
12년 전 조회 727
12년 전 조회 1,345
12년 전 조회 1,663
12년 전 조회 1,318
12년 전 조회 1,925
🐛 버그신고