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

· 12년 전 · 2479 · 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,615
12년 전 조회 1,481
12년 전 조회 4,003
12년 전 조회 1,634
12년 전 조회 960
12년 전 조회 1,644
12년 전 조회 741
12년 전 조회 1,023
12년 전 조회 1,413
12년 전 조회 1,312
12년 전 조회 5,275
12년 전 조회 2,620
12년 전 조회 1,889
12년 전 조회 726
12년 전 조회 2,345
12년 전 조회 937
12년 전 조회 1,254
12년 전 조회 955
12년 전 조회 3,476
12년 전 조회 872
12년 전 조회 3,785
12년 전 조회 1,455
12년 전 조회 1,752
12년 전 조회 2,364
12년 전 조회 2,425
12년 전 조회 685
12년 전 조회 755
12년 전 조회 1,408
12년 전 조회 724
12년 전 조회 914
12년 전 조회 6,669
12년 전 조회 660
12년 전 조회 948
12년 전 조회 595
12년 전 조회 639
12년 전 조회 898
12년 전 조회 848
12년 전 조회 1,830
12년 전 조회 1,824
12년 전 조회 1,471
12년 전 조회 5,343
12년 전 조회 1,608
12년 전 조회 1,706
12년 전 조회 1,058
12년 전 조회 2,075
12년 전 조회 1,469
12년 전 조회 3,137
12년 전 조회 2,655
12년 전 조회 1,997
12년 전 조회 2,480
12년 전 조회 1,960
12년 전 조회 1,724
12년 전 조회 1,139
12년 전 조회 1,241
12년 전 조회 5,649
12년 전 조회 2,707
12년 전 조회 3,179
12년 전 조회 1,835
12년 전 조회 1,557
12년 전 조회 907
12년 전 조회 1,918
12년 전 조회 1,029
12년 전 조회 1,598
12년 전 조회 2,868
12년 전 조회 2,434
12년 전 조회 1,779
12년 전 조회 6,755
12년 전 조회 2,596
12년 전 조회 808
12년 전 조회 767
12년 전 조회 1,500
12년 전 조회 2,162
12년 전 조회 980
12년 전 조회 801
12년 전 조회 1,434
12년 전 조회 1,571
12년 전 조회 790
12년 전 조회 1,233
12년 전 조회 1,509
12년 전 조회 888
12년 전 조회 1,175
12년 전 조회 3,752
12년 전 조회 1,521
12년 전 조회 1,051
12년 전 조회 952
12년 전 조회 1,314
12년 전 조회 1,550
12년 전 조회 4,337
12년 전 조회 5,669
12년 전 조회 803
12년 전 조회 2,364
12년 전 조회 6,838
12년 전 조회 2,493
12년 전 조회 9,431
12년 전 조회 623
12년 전 조회 823
12년 전 조회 1,455
12년 전 조회 1,759
12년 전 조회 1,421
12년 전 조회 2,027
🐛 버그신고