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

· 12년 전 · 2950 · 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,084
12년 전 조회 1,947
12년 전 조회 4,518
12년 전 조회 2,165
12년 전 조회 1,401
12년 전 조회 2,102
12년 전 조회 1,222
12년 전 조회 1,474
12년 전 조회 1,869
12년 전 조회 1,772
12년 전 조회 5,718
12년 전 조회 3,081
12년 전 조회 2,336
12년 전 조회 1,173
12년 전 조회 2,730
12년 전 조회 1,353
12년 전 조회 1,689
12년 전 조회 1,407
12년 전 조회 3,925
12년 전 조회 1,288
12년 전 조회 4,198
12년 전 조회 1,891
12년 전 조회 2,186
12년 전 조회 2,776
12년 전 조회 2,852
12년 전 조회 1,119
12년 전 조회 1,174
12년 전 조회 1,839
12년 전 조회 1,167
12년 전 조회 1,349
12년 전 조회 7,106
12년 전 조회 1,095
12년 전 조회 1,398
12년 전 조회 1,045
12년 전 조회 1,070
12년 전 조회 1,319
12년 전 조회 1,276
12년 전 조회 2,253
12년 전 조회 2,271
12년 전 조회 1,889
12년 전 조회 5,773
12년 전 조회 2,045
12년 전 조회 2,146
12년 전 조회 1,502
12년 전 조회 2,528
12년 전 조회 1,902
12년 전 조회 3,568
12년 전 조회 3,077
12년 전 조회 2,476
12년 전 조회 2,951
12년 전 조회 2,401
12년 전 조회 2,215
12년 전 조회 1,580
12년 전 조회 1,695
12년 전 조회 6,080
12년 전 조회 3,168
12년 전 조회 3,653
12년 전 조회 2,277
12년 전 조회 1,985
12년 전 조회 1,352
12년 전 조회 2,350
12년 전 조회 1,469
12년 전 조회 2,042
12년 전 조회 3,276
12년 전 조회 2,915
12년 전 조회 2,226
12년 전 조회 7,147
12년 전 조회 2,983
12년 전 조회 1,236
12년 전 조회 1,201
12년 전 조회 1,929
12년 전 조회 2,566
12년 전 조회 1,412
12년 전 조회 1,240
12년 전 조회 1,864
12년 전 조회 2,008
12년 전 조회 1,225
12년 전 조회 1,641
12년 전 조회 1,939
12년 전 조회 1,327
12년 전 조회 1,628
12년 전 조회 4,256
12년 전 조회 1,955
12년 전 조회 1,491
12년 전 조회 1,373
12년 전 조회 1,746
12년 전 조회 2,030
12년 전 조회 4,777
12년 전 조회 6,122
12년 전 조회 1,237
12년 전 조회 2,902
12년 전 조회 7,271
12년 전 조회 2,933
12년 전 조회 9,966
12년 전 조회 1,090
12년 전 조회 1,278
12년 전 조회 1,913
12년 전 조회 2,219
12년 전 조회 1,874
12년 전 조회 2,570