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

· 12년 전 · 2385 · 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,518
12년 전 조회 1,368
12년 전 조회 3,901
12년 전 조회 1,521
12년 전 조회 859
12년 전 조회 1,553
12년 전 조회 646
12년 전 조회 918
12년 전 조회 1,313
12년 전 조회 1,197
12년 전 조회 5,170
12년 전 조회 2,525
12년 전 조회 1,787
12년 전 조회 631
12년 전 조회 2,232
12년 전 조회 832
12년 전 조회 1,147
12년 전 조회 863
12년 전 조회 3,370
12년 전 조회 782
12년 전 조회 3,676
12년 전 조회 1,361
12년 전 조회 1,645
12년 전 조회 2,256
12년 전 조회 2,323
12년 전 조회 580
12년 전 조회 655
12년 전 조회 1,312
12년 전 조회 626
12년 전 조회 810
12년 전 조회 6,585
12년 전 조회 565
12년 전 조회 852
12년 전 조회 496
12년 전 조회 537
12년 전 조회 796
12년 전 조회 740
12년 전 조회 1,739
12년 전 조회 1,731
12년 전 조회 1,369
12년 전 조회 5,238
12년 전 조회 1,511
12년 전 조회 1,612
12년 전 조회 963
12년 전 조회 1,980
12년 전 조회 1,378
12년 전 조회 3,046
12년 전 조회 2,551
12년 전 조회 1,908
12년 전 조회 2,386
12년 전 조회 1,862
12년 전 조회 1,633
12년 전 조회 1,032
12년 전 조회 1,142
12년 전 조회 5,563
12년 전 조회 2,609
12년 전 조회 3,092
12년 전 조회 1,747
12년 전 조회 1,464
12년 전 조회 813
12년 전 조회 1,819
12년 전 조회 930
12년 전 조회 1,508
12년 전 조회 2,772
12년 전 조회 2,342
12년 전 조회 1,682
12년 전 조회 6,665
12년 전 조회 2,498
12년 전 조회 714
12년 전 조회 668
12년 전 조회 1,414
12년 전 조회 2,059
12년 전 조회 902
12년 전 조회 708
12년 전 조회 1,340
12년 전 조회 1,475
12년 전 조회 703
12년 전 조회 1,130
12년 전 조회 1,403
12년 전 조회 789
12년 전 조회 1,082
12년 전 조회 3,676
12년 전 조회 1,424
12년 전 조회 957
12년 전 조회 852
12년 전 조회 1,217
12년 전 조회 1,453
12년 전 조회 4,243
12년 전 조회 5,574
12년 전 조회 703
12년 전 조회 2,269
12년 전 조회 6,740
12년 전 조회 2,413
12년 전 조회 9,333
12년 전 조회 538
12년 전 조회 740
12년 전 조회 1,360
12년 전 조회 1,677
12년 전 조회 1,329
12년 전 조회 1,936
🐛 버그신고