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

· 12년 전 · 3772 · 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,950
12년 전 조회 2,768
12년 전 조회 5,361
12년 전 조회 2,994
12년 전 조회 2,271
12년 전 조회 2,958
12년 전 조회 2,078
12년 전 조회 2,305
12년 전 조회 2,714
12년 전 조회 2,606
12년 전 조회 6,598
12년 전 조회 3,956
12년 전 조회 3,222
12년 전 조회 2,004
12년 전 조회 3,538
12년 전 조회 2,183
12년 전 조회 2,498
12년 전 조회 2,244
12년 전 조회 4,770
12년 전 조회 2,128
12년 전 조회 5,009
12년 전 조회 2,720
12년 전 조회 2,992
12년 전 조회 3,601
12년 전 조회 3,678
12년 전 조회 1,922
12년 전 조회 2,022
12년 전 조회 2,672
12년 전 조회 1,996
12년 전 조회 2,139
12년 전 조회 7,938
12년 전 조회 1,930
12년 전 조회 2,209
12년 전 조회 1,864
12년 전 조회 1,897
12년 전 조회 2,146
12년 전 조회 2,122
12년 전 조회 3,092
12년 전 조회 3,123
12년 전 조회 2,710
12년 전 조회 6,602
12년 전 조회 2,882
12년 전 조회 2,967
12년 전 조회 2,353
12년 전 조회 3,341
12년 전 조회 2,755
12년 전 조회 4,436
12년 전 조회 3,942
12년 전 조회 3,284
12년 전 조회 3,773
12년 전 조회 3,222
12년 전 조회 3,036
12년 전 조회 2,381
12년 전 조회 2,520
12년 전 조회 6,963
12년 전 조회 4,010
12년 전 조회 4,480
12년 전 조회 3,116
12년 전 조회 2,825
12년 전 조회 2,209
12년 전 조회 3,199
12년 전 조회 2,323
12년 전 조회 2,839
12년 전 조회 4,091
12년 전 조회 3,713
12년 전 조회 3,056
12년 전 조회 8,005
12년 전 조회 3,861
12년 전 조회 2,064
12년 전 조회 2,030
12년 전 조회 2,768
12년 전 조회 3,390
12년 전 조회 2,226
12년 전 조회 2,127
12년 전 조회 2,733
12년 전 조회 2,912
12년 전 조회 2,096
12년 전 조회 2,542
12년 전 조회 2,836
12년 전 조회 2,195
12년 전 조회 2,532
12년 전 조회 5,128
12년 전 조회 2,853
12년 전 조회 2,373
12년 전 조회 2,262
12년 전 조회 2,680
12년 전 조회 2,912
12년 전 조회 5,704
12년 전 조회 7,033
12년 전 조회 2,169
12년 전 조회 3,616
12년 전 조회 8,196
12년 전 조회 3,856
12년 전 조회 1.1만
12년 전 조회 1,981
12년 전 조회 2,167
12년 전 조회 2,848
12년 전 조회 3,150
12년 전 조회 2,782
12년 전 조회 3,300