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

· 12년 전 · 2366 · 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,505
12년 전 조회 1,355
12년 전 조회 3,885
12년 전 조회 1,506
12년 전 조회 842
12년 전 조회 1,540
12년 전 조회 632
12년 전 조회 904
12년 전 조회 1,297
12년 전 조회 1,181
12년 전 조회 5,154
12년 전 조회 2,509
12년 전 조회 1,770
12년 전 조회 620
12년 전 조회 2,224
12년 전 조회 815
12년 전 조회 1,131
12년 전 조회 846
12년 전 조회 3,360
12년 전 조회 766
12년 전 조회 3,660
12년 전 조회 1,344
12년 전 조회 1,629
12년 전 조회 2,243
12년 전 조회 2,308
12년 전 조회 564
12년 전 조회 643
12년 전 조회 1,297
12년 전 조회 615
12년 전 조회 792
12년 전 조회 6,577
12년 전 조회 548
12년 전 조회 839
12년 전 조회 478
12년 전 조회 522
12년 전 조회 789
12년 전 조회 725
12년 전 조회 1,724
12년 전 조회 1,715
12년 전 조회 1,353
12년 전 조회 5,224
12년 전 조회 1,496
12년 전 조회 1,595
12년 전 조회 950
12년 전 조회 1,969
12년 전 조회 1,363
12년 전 조회 3,037
12년 전 조회 2,543
12년 전 조회 1,890
12년 전 조회 2,367
12년 전 조회 1,847
12년 전 조회 1,621
12년 전 조회 1,017
12년 전 조회 1,127
12년 전 조회 5,549
12년 전 조회 2,592
12년 전 조회 3,074
12년 전 조회 1,740
12년 전 조회 1,455
12년 전 조회 796
12년 전 조회 1,802
12년 전 조회 915
12년 전 조회 1,493
12년 전 조회 2,753
12년 전 조회 2,325
12년 전 조회 1,669
12년 전 조회 6,650
12년 전 조회 2,489
12년 전 조회 697
12년 전 조회 656
12년 전 조회 1,402
12년 전 조회 2,045
12년 전 조회 885
12년 전 조회 694
12년 전 조회 1,324
12년 전 조회 1,458
12년 전 조회 688
12년 전 조회 1,114
12년 전 조회 1,388
12년 전 조회 779
12년 전 조회 1,066
12년 전 조회 3,663
12년 전 조회 1,412
12년 전 조회 950
12년 전 조회 835
12년 전 조회 1,203
12년 전 조회 1,439
12년 전 조회 4,230
12년 전 조회 5,558
12년 전 조회 686
12년 전 조회 2,258
12년 전 조회 6,725
12년 전 조회 2,397
12년 전 조회 9,319
12년 전 조회 522
12년 전 조회 724
12년 전 조회 1,343
12년 전 조회 1,661
12년 전 조회 1,313
12년 전 조회 1,922
🐛 버그신고