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

· 12년 전 · 3830 · 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년 전 조회 3,014
12년 전 조회 2,839
12년 전 조회 5,425
12년 전 조회 3,046
12년 전 조회 2,320
12년 전 조회 3,007
12년 전 조회 2,140
12년 전 조회 2,370
12년 전 조회 2,777
12년 전 조회 2,666
12년 전 조회 6,656
12년 전 조회 4,018
12년 전 조회 3,272
12년 전 조회 2,059
12년 전 조회 3,596
12년 전 조회 2,241
12년 전 조회 2,544
12년 전 조회 2,301
12년 전 조회 4,824
12년 전 조회 2,177
12년 전 조회 5,055
12년 전 조회 2,768
12년 전 조회 3,049
12년 전 조회 3,656
12년 전 조회 3,727
12년 전 조회 1,974
12년 전 조회 2,068
12년 전 조회 2,721
12년 전 조회 2,048
12년 전 조회 2,197
12년 전 조회 7,992
12년 전 조회 1,990
12년 전 조회 2,260
12년 전 조회 1,923
12년 전 조회 1,949
12년 전 조회 2,199
12년 전 조회 2,188
12년 전 조회 3,156
12년 전 조회 3,174
12년 전 조회 2,750
12년 전 조회 6,659
12년 전 조회 2,943
12년 전 조회 3,022
12년 전 조회 2,417
12년 전 조회 3,409
12년 전 조회 2,815
12년 전 조회 4,503
12년 전 조회 3,998
12년 전 조회 3,354
12년 전 조회 3,831
12년 전 조회 3,300
12년 전 조회 3,089
12년 전 조회 2,455
12년 전 조회 2,581
12년 전 조회 7,018
12년 전 조회 4,078
12년 전 조회 4,550
12년 전 조회 3,172
12년 전 조회 2,887
12년 전 조회 2,286
12년 전 조회 3,265
12년 전 조회 2,371
12년 전 조회 2,898
12년 전 조회 4,151
12년 전 조회 3,780
12년 전 조회 3,123
12년 전 조회 8,063
12년 전 조회 3,918
12년 전 조회 2,134
12년 전 조회 2,103
12년 전 조회 2,830
12년 전 조회 3,450
12년 전 조회 2,281
12년 전 조회 2,188
12년 전 조회 2,792
12년 전 조회 2,956
12년 전 조회 2,168
12년 전 조회 2,593
12년 전 조회 2,890
12년 전 조회 2,257
12년 전 조회 2,597
12년 전 조회 5,208
12년 전 조회 2,923
12년 전 조회 2,422
12년 전 조회 2,322
12년 전 조회 2,749
12년 전 조회 2,975
12년 전 조회 5,775
12년 전 조회 7,106
12년 전 조회 2,232
12년 전 조회 3,647
12년 전 조회 8,259
12년 전 조회 3,924
12년 전 조회 1.1만
12년 전 조회 2,042
12년 전 조회 2,219
12년 전 조회 2,906
12년 전 조회 3,204
12년 전 조회 2,847
12년 전 조회 3,342