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

· 12년 전 · 2416 · 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,549
12년 전 조회 1,408
12년 전 조회 3,935
12년 전 조회 1,564
12년 전 조회 894
12년 전 조회 1,592
12년 전 조회 678
12년 전 조회 955
12년 전 조회 1,350
12년 전 조회 1,241
12년 전 조회 5,208
12년 전 조회 2,563
12년 전 조회 1,828
12년 전 조회 660
12년 전 조회 2,280
12년 전 조회 868
12년 전 조회 1,183
12년 전 조회 900
12년 전 조회 3,405
12년 전 조회 817
12년 전 조회 3,722
12년 전 조회 1,391
12년 전 조회 1,675
12년 전 조회 2,294
12년 전 조회 2,354
12년 전 조회 626
12년 전 조회 687
12년 전 조회 1,344
12년 전 조회 664
12년 전 조회 842
12년 전 조회 6,625
12년 전 조회 594
12년 전 조회 883
12년 전 조회 529
12년 전 조회 571
12년 전 조회 839
12년 전 조회 777
12년 전 조회 1,767
12년 전 조회 1,763
12년 전 조회 1,403
12년 전 조회 5,270
12년 전 조회 1,543
12년 전 조회 1,645
12년 전 조회 991
12년 전 조회 2,003
12년 전 조회 1,405
12년 전 조회 3,073
12년 전 조회 2,588
12년 전 조회 1,938
12년 전 조회 2,417
12년 전 조회 1,890
12년 전 조회 1,668
12년 전 조회 1,062
12년 전 조회 1,179
12년 전 조회 5,593
12년 전 조회 2,643
12년 전 조회 3,118
12년 전 조회 1,783
12년 전 조회 1,493
12년 전 조회 845
12년 전 조회 1,857
12년 전 조회 964
12년 전 조회 1,535
12년 전 조회 2,809
12년 전 조회 2,371
12년 전 조회 1,720
12년 전 조회 6,696
12년 전 조회 2,530
12년 전 조회 737
12년 전 조회 699
12년 전 조회 1,447
12년 전 조회 2,086
12년 전 조회 924
12년 전 조회 734
12년 전 조회 1,375
12년 전 조회 1,508
12년 전 조회 732
12년 전 조회 1,160
12년 전 조회 1,435
12년 전 조회 823
12년 전 조회 1,115
12년 전 조회 3,705
12년 전 조회 1,453
12년 전 조회 989
12년 전 조회 882
12년 전 조회 1,252
12년 전 조회 1,483
12년 전 조회 4,277
12년 전 조회 5,605
12년 전 조회 739
12년 전 조회 2,300
12년 전 조회 6,777
12년 전 조회 2,439
12년 전 조회 9,368
12년 전 조회 569
12년 전 조회 767
12년 전 조회 1,392
12년 전 조회 1,696
12년 전 조회 1,353
12년 전 조회 1,965
🐛 버그신고