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

· 12년 전 · 2947 · 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,083
12년 전 조회 1,944
12년 전 조회 4,514
12년 전 조회 2,163
12년 전 조회 1,399
12년 전 조회 2,097
12년 전 조회 1,220
12년 전 조회 1,472
12년 전 조회 1,863
12년 전 조회 1,767
12년 전 조회 5,715
12년 전 조회 3,079
12년 전 조회 2,330
12년 전 조회 1,169
12년 전 조회 2,727
12년 전 조회 1,346
12년 전 조회 1,685
12년 전 조회 1,405
12년 전 조회 3,924
12년 전 조회 1,285
12년 전 조회 4,191
12년 전 조회 1,886
12년 전 조회 2,183
12년 전 조회 2,771
12년 전 조회 2,848
12년 전 조회 1,114
12년 전 조회 1,167
12년 전 조회 1,835
12년 전 조회 1,165
12년 전 조회 1,344
12년 전 조회 7,104
12년 전 조회 1,085
12년 전 조회 1,392
12년 전 조회 1,040
12년 전 조회 1,067
12년 전 조회 1,315
12년 전 조회 1,270
12년 전 조회 2,251
12년 전 조회 2,267
12년 전 조회 1,886
12년 전 조회 5,771
12년 전 조회 2,041
12년 전 조회 2,145
12년 전 조회 1,501
12년 전 조회 2,524
12년 전 조회 1,897
12년 전 조회 3,565
12년 전 조회 3,076
12년 전 조회 2,473
12년 전 조회 2,948
12년 전 조회 2,398
12년 전 조회 2,213
12년 전 조회 1,574
12년 전 조회 1,688
12년 전 조회 6,076
12년 전 조회 3,164
12년 전 조회 3,650
12년 전 조회 2,276
12년 전 조회 1,981
12년 전 조회 1,340
12년 전 조회 2,346
12년 전 조회 1,466
12년 전 조회 2,035
12년 전 조회 3,275
12년 전 조회 2,914
12년 전 조회 2,226
12년 전 조회 7,144
12년 전 조회 2,981
12년 전 조회 1,229
12년 전 조회 1,196
12년 전 조회 1,925
12년 전 조회 2,562
12년 전 조회 1,408
12년 전 조회 1,235
12년 전 조회 1,861
12년 전 조회 2,004
12년 전 조회 1,221
12년 전 조회 1,639
12년 전 조회 1,932
12년 전 조회 1,321
12년 전 조회 1,618
12년 전 조회 4,251
12년 전 조회 1,950
12년 전 조회 1,485
12년 전 조회 1,369
12년 전 조회 1,740
12년 전 조회 2,026
12년 전 조회 4,776
12년 전 조회 6,121
12년 전 조회 1,233
12년 전 조회 2,896
12년 전 조회 7,269
12년 전 조회 2,932
12년 전 조회 9,962
12년 전 조회 1,083
12년 전 조회 1,273
12년 전 조회 1,905
12년 전 조회 2,210
12년 전 조회 1,866
12년 전 조회 2,558