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

· 12년 전 · 2573 · 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,711
12년 전 조회 1,561
12년 전 조회 4,093
12년 전 조회 1,716
12년 전 조회 1,051
12년 전 조회 1,729
12년 전 조회 843
12년 전 조회 1,114
12년 전 조회 1,507
12년 전 조회 1,394
12년 전 조회 5,347
12년 전 조회 2,700
12년 전 조회 1,980
12년 전 조회 820
12년 전 조회 2,409
12년 전 조회 1,022
12년 전 조회 1,328
12년 전 조회 1,048
12년 전 조회 3,550
12년 전 조회 953
12년 전 조회 3,868
12년 전 조회 1,547
12년 전 조회 1,834
12년 전 조회 2,440
12년 전 조회 2,512
12년 전 조회 762
12년 전 조회 837
12년 전 조회 1,498
12년 전 조회 815
12년 전 조회 997
12년 전 조회 6,761
12년 전 조회 751
12년 전 조회 1,052
12년 전 조회 696
12년 전 조회 719
12년 전 조회 972
12년 전 조회 924
12년 전 조회 1,923
12년 전 조회 1,918
12년 전 조회 1,550
12년 전 조회 5,424
12년 전 조회 1,702
12년 전 조회 1,793
12년 전 조회 1,154
12년 전 조회 2,162
12년 전 조회 1,570
12년 전 조회 3,212
12년 전 조회 2,732
12년 전 조회 2,097
12년 전 조회 2,574
12년 전 조회 2,038
12년 전 조회 1,816
12년 전 조회 1,222
12년 전 조회 1,335
12년 전 조회 5,731
12년 전 조회 2,797
12년 전 조회 3,278
12년 전 조회 1,914
12년 전 조회 1,634
12년 전 조회 1,003
12년 전 조회 2,015
12년 전 조회 1,121
12년 전 조회 1,704
12년 전 조회 2,956
12년 전 조회 2,538
12년 전 조회 1,857
12년 전 조회 6,838
12년 전 조회 2,673
12년 전 조회 903
12년 전 조회 855
12년 전 조회 1,588
12년 전 조회 2,245
12년 전 조회 1,070
12년 전 조회 900
12년 전 조회 1,523
12년 전 조회 1,658
12년 전 조회 889
12년 전 조회 1,301
12년 전 조회 1,588
12년 전 조회 985
12년 전 조회 1,265
12년 전 조회 3,866
12년 전 조회 1,612
12년 전 조회 1,140
12년 전 조회 1,036
12년 전 조회 1,414
12년 전 조회 1,640
12년 전 조회 4,416
12년 전 조회 5,762
12년 전 조회 890
12년 전 조회 2,451
12년 전 조회 6,911
12년 전 조회 2,574
12년 전 조회 9,522
12년 전 조회 726
12년 전 조회 922
12년 전 조회 1,540
12년 전 조회 1,860
12년 전 조회 1,513
12년 전 조회 2,135
🐛 버그신고