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

· 12년 전 · 2418 · 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,552
12년 전 조회 1,409
12년 전 조회 3,936
12년 전 조회 1,566
12년 전 조회 894
12년 전 조회 1,596
12년 전 조회 678
12년 전 조회 957
12년 전 조회 1,351
12년 전 조회 1,243
12년 전 조회 5,212
12년 전 조회 2,563
12년 전 조회 1,829
12년 전 조회 661
12년 전 조회 2,281
12년 전 조회 869
12년 전 조회 1,189
12년 전 조회 903
12년 전 조회 3,406
12년 전 조회 818
12년 전 조회 3,724
12년 전 조회 1,393
12년 전 조회 1,675
12년 전 조회 2,297
12년 전 조회 2,355
12년 전 조회 628
12년 전 조회 691
12년 전 조회 1,346
12년 전 조회 667
12년 전 조회 845
12년 전 조회 6,626
12년 전 조회 595
12년 전 조회 883
12년 전 조회 529
12년 전 조회 572
12년 전 조회 841
12년 전 조회 778
12년 전 조회 1,768
12년 전 조회 1,766
12년 전 조회 1,404
12년 전 조회 5,273
12년 전 조회 1,546
12년 전 조회 1,646
12년 전 조회 992
12년 전 조회 2,005
12년 전 조회 1,406
12년 전 조회 3,075
12년 전 조회 2,591
12년 전 조회 1,938
12년 전 조회 2,419
12년 전 조회 1,890
12년 전 조회 1,668
12년 전 조회 1,065
12년 전 조회 1,179
12년 전 조회 5,594
12년 전 조회 2,648
12년 전 조회 3,121
12년 전 조회 1,784
12년 전 조회 1,496
12년 전 조회 846
12년 전 조회 1,861
12년 전 조회 965
12년 전 조회 1,537
12년 전 조회 2,810
12년 전 조회 2,372
12년 전 조회 1,721
12년 전 조회 6,697
12년 전 조회 2,532
12년 전 조회 738
12년 전 조회 701
12년 전 조회 1,447
12년 전 조회 2,090
12년 전 조회 927
12년 전 조회 735
12년 전 조회 1,375
12년 전 조회 1,510
12년 전 조회 733
12년 전 조회 1,162
12년 전 조회 1,437
12년 전 조회 826
12년 전 조회 1,118
12년 전 조회 3,706
12년 전 조회 1,456
12년 전 조회 989
12년 전 조회 885
12년 전 조회 1,252
12년 전 조회 1,483
12년 전 조회 4,278
12년 전 조회 5,605
12년 전 조회 742
12년 전 조회 2,300
12년 전 조회 6,780
12년 전 조회 2,440
12년 전 조회 9,370
12년 전 조회 570
12년 전 조회 767
12년 전 조회 1,393
12년 전 조회 1,697
12년 전 조회 1,354
12년 전 조회 1,966
🐛 버그신고