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

· 12년 전 · 3872 · 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,060
12년 전 조회 2,873
12년 전 조회 5,466
12년 전 조회 3,084
12년 전 조회 2,360
12년 전 조회 3,051
12년 전 조회 2,183
12년 전 조회 2,422
12년 전 조회 2,808
12년 전 조회 2,707
12년 전 조회 6,689
12년 전 조회 4,057
12년 전 조회 3,305
12년 전 조회 2,094
12년 전 조회 3,636
12년 전 조회 2,275
12년 전 조회 2,577
12년 전 조회 2,338
12년 전 조회 4,877
12년 전 조회 2,223
12년 전 조회 5,092
12년 전 조회 2,814
12년 전 조회 3,093
12년 전 조회 3,699
12년 전 조회 3,767
12년 전 조회 2,014
12년 전 조회 2,109
12년 전 조회 2,757
12년 전 조회 2,086
12년 전 조회 2,240
12년 전 조회 8,030
12년 전 조회 2,034
12년 전 조회 2,301
12년 전 조회 1,952
12년 전 조회 1,981
12년 전 조회 2,241
12년 전 조회 2,225
12년 전 조회 3,196
12년 전 조회 3,217
12년 전 조회 2,798
12년 전 조회 6,707
12년 전 조회 2,973
12년 전 조회 3,068
12년 전 조회 2,455
12년 전 조회 3,443
12년 전 조회 2,853
12년 전 조회 4,540
12년 전 조회 4,041
12년 전 조회 3,387
12년 전 조회 3,873
12년 전 조회 3,332
12년 전 조회 3,124
12년 전 조회 2,493
12년 전 조회 2,618
12년 전 조회 7,051
12년 전 조회 4,115
12년 전 조회 4,584
12년 전 조회 3,209
12년 전 조회 2,917
12년 전 조회 2,315
12년 전 조회 3,302
12년 전 조회 2,414
12년 전 조회 2,945
12년 전 조회 4,194
12년 전 조회 3,831
12년 전 조회 3,161
12년 전 조회 8,105
12년 전 조회 3,953
12년 전 조회 2,177
12년 전 조회 2,149
12년 전 조회 2,880
12년 전 조회 3,494
12년 전 조회 2,333
12년 전 조회 2,235
12년 전 조회 2,845
12년 전 조회 3,010
12년 전 조회 2,207
12년 전 조회 2,639
12년 전 조회 2,954
12년 전 조회 2,312
12년 전 조회 2,653
12년 전 조회 5,260
12년 전 조회 2,976
12년 전 조회 2,467
12년 전 조회 2,370
12년 전 조회 2,798
12년 전 조회 3,031
12년 전 조회 5,834
12년 전 조회 7,151
12년 전 조회 2,284
12년 전 조회 3,667
12년 전 조회 8,316
12년 전 조회 3,978
12년 전 조회 1.1만
12년 전 조회 2,100
12년 전 조회 2,271
12년 전 조회 2,961
12년 전 조회 3,267
12년 전 조회 2,893
12년 전 조회 3,354