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

· 12년 전 · 3856 · 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,037
12년 전 조회 2,860
12년 전 조회 5,455
12년 전 조회 3,073
12년 전 조회 2,346
12년 전 조회 3,031
12년 전 조회 2,165
12년 전 조회 2,401
12년 전 조회 2,797
12년 전 조회 2,685
12년 전 조회 6,674
12년 전 조회 4,042
12년 전 조회 3,296
12년 전 조회 2,077
12년 전 조회 3,616
12년 전 조회 2,262
12년 전 조회 2,564
12년 전 조회 2,324
12년 전 조회 4,861
12년 전 조회 2,206
12년 전 조회 5,082
12년 전 조회 2,802
12년 전 조회 3,080
12년 전 조회 3,680
12년 전 조회 3,755
12년 전 조회 2,004
12년 전 조회 2,094
12년 전 조회 2,745
12년 전 조회 2,075
12년 전 조회 2,225
12년 전 조회 8,012
12년 전 조회 2,015
12년 전 조회 2,283
12년 전 조회 1,936
12년 전 조회 1,967
12년 전 조회 2,221
12년 전 조회 2,211
12년 전 조회 3,177
12년 전 조회 3,200
12년 전 조회 2,781
12년 전 조회 6,690
12년 전 조회 2,960
12년 전 조회 3,048
12년 전 조회 2,443
12년 전 조회 3,426
12년 전 조회 2,837
12년 전 조회 4,528
12년 전 조회 4,025
12년 전 조회 3,372
12년 전 조회 3,857
12년 전 조회 3,323
12년 전 조회 3,107
12년 전 조회 2,478
12년 전 조회 2,606
12년 전 조회 7,040
12년 전 조회 4,103
12년 전 조회 4,570
12년 전 조회 3,191
12년 전 조회 2,905
12년 전 조회 2,299
12년 전 조회 3,282
12년 전 조회 2,402
12년 전 조회 2,927
12년 전 조회 4,174
12년 전 조회 3,814
12년 전 조회 3,147
12년 전 조회 8,090
12년 전 조회 3,943
12년 전 조회 2,158
12년 전 조회 2,131
12년 전 조회 2,860
12년 전 조회 3,477
12년 전 조회 2,314
12년 전 조회 2,218
12년 전 조회 2,821
12년 전 조회 2,991
12년 전 조회 2,194
12년 전 조회 2,617
12년 전 조회 2,927
12년 전 조회 2,289
12년 전 조회 2,625
12년 전 조회 5,239
12년 전 조회 2,957
12년 전 조회 2,452
12년 전 조회 2,349
12년 전 조회 2,780
12년 전 조회 3,014
12년 전 조회 5,810
12년 전 조회 7,135
12년 전 조회 2,261
12년 전 조회 3,662
12년 전 조회 8,297
12년 전 조회 3,959
12년 전 조회 1.1만
12년 전 조회 2,075
12년 전 조회 2,243
12년 전 조회 2,936
12년 전 조회 3,246
12년 전 조회 2,874
12년 전 조회 3,351