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

· 12년 전 · 3625 · 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년 전 조회 2,810
12년 전 조회 2,663
12년 전 조회 5,236
12년 전 조회 2,864
12년 전 조회 2,136
12년 전 조회 2,836
12년 전 조회 1,957
12년 전 조회 2,190
12년 전 조회 2,620
12년 전 조회 2,462
12년 전 조회 6,478
12년 전 조회 3,833
12년 전 조회 3,120
12년 전 조회 1,883
12년 전 조회 3,390
12년 전 조회 2,066
12년 전 조회 2,372
12년 전 조회 2,123
12년 전 조회 4,658
12년 전 조회 2,007
12년 전 조회 4,869
12년 전 조회 2,599
12년 전 조회 2,858
12년 전 조회 3,490
12년 전 조회 3,552
12년 전 조회 1,799
12년 전 조회 1,897
12년 전 조회 2,550
12년 전 조회 1,884
12년 전 조회 2,028
12년 전 조회 7,832
12년 전 조회 1,804
12년 전 조회 2,083
12년 전 조회 1,743
12년 전 조회 1,775
12년 전 조회 2,019
12년 전 조회 2,003
12년 전 조회 2,963
12년 전 조회 2,991
12년 전 조회 2,591
12년 전 조회 6,460
12년 전 조회 2,756
12년 전 조회 2,842
12년 전 조회 2,238
12년 전 조회 3,233
12년 전 조회 2,637
12년 전 조회 4,311
12년 전 조회 3,802
12년 전 조회 3,147
12년 전 조회 3,626
12년 전 조회 3,079
12년 전 조회 2,896
12년 전 조회 2,251
12년 전 조회 2,386
12년 전 조회 6,813
12년 전 조회 3,874
12년 전 조회 4,347
12년 전 조회 2,986
12년 전 조회 2,699
12년 전 조회 2,073
12년 전 조회 3,061
12년 전 조회 2,203
12년 전 조회 2,711
12년 전 조회 3,939
12년 전 조회 3,563
12년 전 조회 2,918
12년 전 조회 7,846
12년 전 조회 3,708
12년 전 조회 1,920
12년 전 조회 1,895
12년 전 조회 2,618
12년 전 조회 3,250
12년 전 조회 2,096
12년 전 조회 1,992
12년 전 조회 2,594
12년 전 조회 2,776
12년 전 조회 1,961
12년 전 조회 2,393
12년 전 조회 2,708
12년 전 조회 2,062
12년 전 조회 2,401
12년 전 조회 4,988
12년 전 조회 2,747
12년 전 조회 2,256
12년 전 조회 2,131
12년 전 조회 2,543
12년 전 조회 2,785
12년 전 조회 5,570
12년 전 조회 6,900
12년 전 조회 2,036
12년 전 조회 3,550
12년 전 조회 8,059
12년 전 조회 3,718
12년 전 조회 1.1만
12년 전 조회 1,863
12년 전 조회 2,047
12년 전 조회 2,730
12년 전 조회 3,041
12년 전 조회 2,660
12년 전 조회 3,234