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

· 12년 전 · 2578 · 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년 전 조회 1,716
12년 전 조회 1,564
12년 전 조회 4,098
12년 전 조회 1,722
12년 전 조회 1,054
12년 전 조회 1,734
12년 전 조회 847
12년 전 조회 1,116
12년 전 조회 1,509
12년 전 조회 1,397
12년 전 조회 5,350
12년 전 조회 2,700
12년 전 조회 1,984
12년 전 조회 823
12년 전 조회 2,410
12년 전 조회 1,026
12년 전 조회 1,334
12년 전 조회 1,051
12년 전 조회 3,553
12년 전 조회 954
12년 전 조회 3,870
12년 전 조회 1,549
12년 전 조회 1,840
12년 전 조회 2,446
12년 전 조회 2,516
12년 전 조회 766
12년 전 조회 842
12년 전 조회 1,501
12년 전 조회 818
12년 전 조회 1,002
12년 전 조회 6,762
12년 전 조회 757
12년 전 조회 1,056
12년 전 조회 698
12년 전 조회 720
12년 전 조회 974
12년 전 조회 927
12년 전 조회 1,931
12년 전 조회 1,923
12년 전 조회 1,553
12년 전 조회 5,429
12년 전 조회 1,706
12년 전 조회 1,797
12년 전 조회 1,160
12년 전 조회 2,164
12년 전 조회 1,574
12년 전 조회 3,215
12년 전 조회 2,734
12년 전 조회 2,102
12년 전 조회 2,579
12년 전 조회 2,042
12년 전 조회 1,817
12년 전 조회 1,226
12년 전 조회 1,340
12년 전 조회 5,733
12년 전 조회 2,804
12년 전 조회 3,280
12년 전 조회 1,915
12년 전 조회 1,636
12년 전 조회 1,007
12년 전 조회 2,019
12년 전 조회 1,123
12년 전 조회 1,712
12년 전 조회 2,959
12년 전 조회 2,541
12년 전 조회 1,860
12년 전 조회 6,842
12년 전 조회 2,673
12년 전 조회 909
12년 전 조회 857
12년 전 조회 1,592
12년 전 조회 2,247
12년 전 조회 1,077
12년 전 조회 905
12년 전 조회 1,526
12년 전 조회 1,665
12년 전 조회 890
12년 전 조회 1,301
12년 전 조회 1,591
12년 전 조회 986
12년 전 조회 1,269
12년 전 조회 3,872
12년 전 조회 1,616
12년 전 조회 1,144
12년 전 조회 1,041
12년 전 조회 1,417
12년 전 조회 1,645
12년 전 조회 4,421
12년 전 조회 5,763
12년 전 조회 896
12년 전 조회 2,453
12년 전 조회 6,913
12년 전 조회 2,577
12년 전 조회 9,524
12년 전 조회 727
12년 전 조회 928
12년 전 조회 1,541
12년 전 조회 1,863
12년 전 조회 1,516
12년 전 조회 2,140
🐛 버그신고