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

· 12년 전 · 3132 · 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,289
12년 전 조회 2,160
12년 전 조회 4,846
12년 전 조회 2,464
12년 전 조회 1,607
12년 전 조회 2,312
12년 전 조회 1,428
12년 전 조회 1,692
12년 전 조회 2,085
12년 전 조회 1,982
12년 전 조회 5,916
12년 전 조회 3,284
12년 전 조회 2,555
12년 전 조회 1,348
12년 전 조회 2,906
12년 전 조회 1,536
12년 전 조회 1,857
12년 전 조회 1,583
12년 전 조회 4,083
12년 전 조회 1,448
12년 전 조회 4,347
12년 전 조회 2,072
12년 전 조회 2,331
12년 전 조회 2,934
12년 전 조회 3,000
12년 전 조회 1,282
12년 전 조회 1,334
12년 전 조회 2,003
12년 전 조회 1,327
12년 전 조회 1,517
12년 전 조회 7,266
12년 전 조회 1,254
12년 전 조회 1,567
12년 전 조회 1,205
12년 전 조회 1,239
12년 전 조회 1,486
12년 전 조회 1,449
12년 전 조회 2,427
12년 전 조회 2,437
12년 전 조회 2,061
12년 전 조회 5,942
12년 전 조회 2,218
12년 전 조회 2,320
12년 전 조회 1,681
12년 전 조회 2,695
12년 전 조회 2,091
12년 전 조회 3,757
12년 전 조회 3,262
12년 전 조회 2,653
12년 전 조회 3,133
12년 전 조회 2,599
12년 전 조회 2,395
12년 전 조회 1,753
12년 전 조회 1,894
12년 전 조회 6,270
12년 전 조회 3,355
12년 전 조회 3,849
12년 전 조회 2,456
12년 전 조회 2,155
12년 전 조회 1,537
12년 전 조회 2,534
12년 전 조회 1,653
12년 전 조회 2,241
12년 전 조회 3,453
12년 전 조회 3,081
12년 전 조회 2,402
12년 전 조회 7,343
12년 전 조회 3,152
12년 전 조회 1,428
12년 전 조회 1,376
12년 전 조회 2,113
12년 전 조회 2,743
12년 전 조회 1,600
12년 전 조회 1,453
12년 전 조회 2,075
12년 전 조회 2,229
12년 전 조회 1,412
12년 전 조회 1,851
12년 전 조회 2,165
12년 전 조회 1,526
12년 전 조회 1,856
12년 전 조회 4,487
12년 전 조회 2,189
12년 전 조회 1,702
12년 전 조회 1,606
12년 전 조회 1,971
12년 전 조회 2,229
12년 전 조회 5,004
12년 전 조회 6,334
12년 전 조회 1,452
12년 전 조회 3,175
12년 전 조회 7,488
12년 전 조회 3,143
12년 전 조회 1만
12년 전 조회 1,282
12년 전 조회 1,497
12년 전 조회 2,125
12년 전 조회 2,442
12년 전 조회 2,084
12년 전 조회 2,863