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

· 12년 전 · 3623 · 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,808
12년 전 조회 2,660
12년 전 조회 5,233
12년 전 조회 2,860
12년 전 조회 2,135
12년 전 조회 2,836
12년 전 조회 1,956
12년 전 조회 2,186
12년 전 조회 2,617
12년 전 조회 2,460
12년 전 조회 6,476
12년 전 조회 3,829
12년 전 조회 3,118
12년 전 조회 1,880
12년 전 조회 3,388
12년 전 조회 2,063
12년 전 조회 2,372
12년 전 조회 2,121
12년 전 조회 4,654
12년 전 조회 2,004
12년 전 조회 4,867
12년 전 조회 2,599
12년 전 조회 2,855
12년 전 조회 3,488
12년 전 조회 3,550
12년 전 조회 1,797
12년 전 조회 1,895
12년 전 조회 2,548
12년 전 조회 1,882
12년 전 조회 2,025
12년 전 조회 7,830
12년 전 조회 1,803
12년 전 조회 2,082
12년 전 조회 1,741
12년 전 조회 1,773
12년 전 조회 2,014
12년 전 조회 2,003
12년 전 조회 2,962
12년 전 조회 2,988
12년 전 조회 2,589
12년 전 조회 6,455
12년 전 조회 2,754
12년 전 조회 2,839
12년 전 조회 2,237
12년 전 조회 3,230
12년 전 조회 2,633
12년 전 조회 4,308
12년 전 조회 3,799
12년 전 조회 3,146
12년 전 조회 3,624
12년 전 조회 3,076
12년 전 조회 2,892
12년 전 조회 2,246
12년 전 조회 2,385
12년 전 조회 6,812
12년 전 조회 3,871
12년 전 조회 4,345
12년 전 조회 2,983
12년 전 조회 2,697
12년 전 조회 2,072
12년 전 조회 3,058
12년 전 조회 2,200
12년 전 조회 2,709
12년 전 조회 3,937
12년 전 조회 3,561
12년 전 조회 2,916
12년 전 조회 7,842
12년 전 조회 3,705
12년 전 조회 1,918
12년 전 조회 1,892
12년 전 조회 2,613
12년 전 조회 3,247
12년 전 조회 2,093
12년 전 조회 1,989
12년 전 조회 2,591
12년 전 조회 2,772
12년 전 조회 1,960
12년 전 조회 2,391
12년 전 조회 2,705
12년 전 조회 2,060
12년 전 조회 2,399
12년 전 조회 4,988
12년 전 조회 2,745
12년 전 조회 2,255
12년 전 조회 2,128
12년 전 조회 2,538
12년 전 조회 2,781
12년 전 조회 5,566
12년 전 조회 6,898
12년 전 조회 2,032
12년 전 조회 3,549
12년 전 조회 8,056
12년 전 조회 3,716
12년 전 조회 1.1만
12년 전 조회 1,863
12년 전 조회 2,046
12년 전 조회 2,728
12년 전 조회 3,039
12년 전 조회 2,656
12년 전 조회 3,234