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

· 12년 전 · 3590 · 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,769
12년 전 조회 2,628
12년 전 조회 5,196
12년 전 조회 2,827
12년 전 조회 2,105
12년 전 조회 2,806
12년 전 조회 1,917
12년 전 조회 2,155
12년 전 조회 2,585
12년 전 조회 2,438
12년 전 조회 6,450
12년 전 조회 3,797
12년 전 조회 3,093
12년 전 조회 1,833
12년 전 조회 3,361
12년 전 조회 2,032
12년 전 조회 2,326
12년 전 조회 2,080
12년 전 조회 4,617
12년 전 조회 1,960
12년 전 조회 4,842
12년 전 조회 2,564
12년 전 조회 2,814
12년 전 조회 3,454
12년 전 조회 3,515
12년 전 조회 1,765
12년 전 조회 1,854
12년 전 조회 2,503
12년 전 조회 1,851
12년 전 조회 1,994
12년 전 조회 7,799
12년 전 조회 1,764
12년 전 조회 2,053
12년 전 조회 1,698
12년 전 조회 1,731
12년 전 조회 1,984
12년 전 조회 1,969
12년 전 조회 2,935
12년 전 조회 2,965
12년 전 조회 2,556
12년 전 조회 6,426
12년 전 조회 2,720
12년 전 조회 2,810
12년 전 조회 2,187
12년 전 조회 3,197
12년 전 조회 2,590
12년 전 조회 4,283
12년 전 조회 3,756
12년 전 조회 3,112
12년 전 조회 3,591
12년 전 조회 3,042
12년 전 조회 2,866
12년 전 조회 2,207
12년 전 조회 2,341
12년 전 조회 6,777
12년 전 조회 3,837
12년 전 조회 4,306
12년 전 조회 2,948
12년 전 조회 2,669
12년 전 조회 2,037
12년 전 조회 3,019
12년 전 조회 2,152
12년 전 조회 2,660
12년 전 조회 3,905
12년 전 조회 3,530
12년 전 조회 2,876
12년 전 조회 7,809
12년 전 조회 3,666
12년 전 조회 1,884
12년 전 조회 1,850
12년 전 조회 2,577
12년 전 조회 3,211
12년 전 조회 2,057
12년 전 조회 1,959
12년 전 조회 2,551
12년 전 조회 2,735
12년 전 조회 1,922
12년 전 조회 2,355
12년 전 조회 2,666
12년 전 조회 2,023
12년 전 조회 2,368
12년 전 조회 4,955
12년 전 조회 2,702
12년 전 조회 2,209
12년 전 조회 2,099
12년 전 조회 2,491
12년 전 조회 2,743
12년 전 조회 5,523
12년 전 조회 6,864
12년 전 조회 1,985
12년 전 조회 3,545
12년 전 조회 8,015
12년 전 조회 3,686
12년 전 조회 1.1만
12년 전 조회 1,813
12년 전 조회 2,004
12년 전 조회 2,674
12년 전 조회 2,992
12년 전 조회 2,619
12년 전 조회 3,222