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

· 12년 전 · 2893 · 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,007
12년 전 조회 1,875
12년 전 조회 4,421
12년 전 조회 2,070
12년 전 조회 1,330
12년 전 조회 2,020
12년 전 조회 1,139
12년 전 조회 1,406
12년 전 조회 1,798
12년 전 조회 1,710
12년 전 조회 5,651
12년 전 조회 3,010
12년 전 조회 2,255
12년 전 조회 1,101
12년 전 조회 2,678
12년 전 조회 1,288
12년 전 조회 1,619
12년 전 조회 1,336
12년 전 조회 3,857
12년 전 조회 1,219
12년 전 조회 4,139
12년 전 조회 1,834
12년 전 조회 2,135
12년 전 조회 2,720
12년 전 조회 2,793
12년 전 조회 1,058
12년 전 조회 1,120
12년 전 조회 1,774
12년 전 조회 1,110
12년 전 조회 1,283
12년 전 조회 7,056
12년 전 조회 1,020
12년 전 조회 1,334
12년 전 조회 981
12년 전 조회 1,005
12년 전 조회 1,259
12년 전 조회 1,209
12년 전 조회 2,199
12년 전 조회 2,210
12년 전 조회 1,828
12년 전 조회 5,702
12년 전 조회 1,978
12년 전 조회 2,090
12년 전 조회 1,441
12년 전 조회 2,465
12년 전 조회 1,842
12년 전 조회 3,503
12년 전 조회 3,013
12년 전 조회 2,412
12년 전 조회 2,894
12년 전 조회 2,340
12년 전 조회 2,150
12년 전 조회 1,499
12년 전 조회 1,630
12년 전 조회 6,020
12년 전 조회 3,105
12년 전 조회 3,599
12년 전 조회 2,223
12년 전 조회 1,935
12년 전 조회 1,277
12년 전 조회 2,292
12년 전 조회 1,409
12년 전 조회 1,969
12년 전 조회 3,219
12년 전 조회 2,844
12년 전 조회 2,169
12년 전 조회 7,088
12년 전 조회 2,926
12년 전 조회 1,166
12년 전 조회 1,138
12년 전 조회 1,860
12년 전 조회 2,517
12년 전 조회 1,347
12년 전 조회 1,177
12년 전 조회 1,806
12년 전 조회 1,940
12년 전 조회 1,169
12년 전 조회 1,583
12년 전 조회 1,865
12년 전 조회 1,254
12년 전 조회 1,549
12년 전 조회 4,198
12년 전 조회 1,880
12년 전 조회 1,424
12년 전 조회 1,308
12년 전 조회 1,684
12년 전 조회 1,965
12년 전 조회 4,719
12년 전 조회 6,056
12년 전 조회 1,160
12년 전 조회 2,801
12년 전 조회 7,201
12년 전 조회 2,862
12년 전 조회 9,872
12년 전 조회 1,017
12년 전 조회 1,218
12년 전 조회 1,843
12년 전 조회 2,141
12년 전 조회 1,796
12년 전 조회 2,470