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

· 12년 전 · 2372 · 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,509
12년 전 조회 1,357
12년 전 조회 3,888
12년 전 조회 1,509
12년 전 조회 850
12년 전 조회 1,545
12년 전 조회 633
12년 전 조회 908
12년 전 조회 1,299
12년 전 조회 1,185
12년 전 조회 5,155
12년 전 조회 2,511
12년 전 조회 1,773
12년 전 조회 621
12년 전 조회 2,224
12년 전 조회 823
12년 전 조회 1,134
12년 전 조회 849
12년 전 조회 3,363
12년 전 조회 768
12년 전 조회 3,666
12년 전 조회 1,351
12년 전 조회 1,634
12년 전 조회 2,244
12년 전 조회 2,312
12년 전 조회 570
12년 전 조회 645
12년 전 조회 1,303
12년 전 조회 618
12년 전 조회 798
12년 전 조회 6,578
12년 전 조회 553
12년 전 조회 843
12년 전 조회 485
12년 전 조회 526
12년 전 조회 790
12년 전 조회 729
12년 전 조회 1,728
12년 전 조회 1,719
12년 전 조회 1,357
12년 전 조회 5,230
12년 전 조회 1,499
12년 전 조회 1,603
12년 전 조회 952
12년 전 조회 1,971
12년 전 조회 1,366
12년 전 조회 3,039
12년 전 조회 2,544
12년 전 조회 1,894
12년 전 조회 2,373
12년 전 조회 1,850
12년 전 조회 1,623
12년 전 조회 1,021
12년 전 조회 1,132
12년 전 조회 5,551
12년 전 조회 2,598
12년 전 조회 3,078
12년 전 조회 1,742
12년 전 조회 1,456
12년 전 조회 801
12년 전 조회 1,809
12년 전 조회 918
12년 전 조회 1,496
12년 전 조회 2,755
12년 전 조회 2,334
12년 전 조회 1,671
12년 전 조회 6,654
12년 전 조회 2,492
12년 전 조회 703
12년 전 조회 658
12년 전 조회 1,405
12년 전 조회 2,046
12년 전 조회 891
12년 전 조회 700
12년 전 조회 1,328
12년 전 조회 1,462
12년 전 조회 691
12년 전 조회 1,116
12년 전 조회 1,392
12년 전 조회 782
12년 전 조회 1,068
12년 전 조회 3,667
12년 전 조회 1,418
12년 전 조회 951
12년 전 조회 840
12년 전 조회 1,210
12년 전 조회 1,443
12년 전 조회 4,232
12년 전 조회 5,564
12년 전 조회 691
12년 전 조회 2,261
12년 전 조회 6,728
12년 전 조회 2,399
12년 전 조회 9,322
12년 전 조회 524
12년 전 조회 730
12년 전 조회 1,346
12년 전 조회 1,664
12년 전 조회 1,320
12년 전 조회 1,925
🐛 버그신고