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

· 12년 전 · 2482 · 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,617
12년 전 조회 1,487
12년 전 조회 4,013
12년 전 조회 1,641
12년 전 조회 967
12년 전 조회 1,648
12년 전 조회 744
12년 전 조회 1,027
12년 전 조회 1,420
12년 전 조회 1,317
12년 전 조회 5,278
12년 전 조회 2,622
12년 전 조회 1,895
12년 전 조회 734
12년 전 조회 2,349
12년 전 조회 944
12년 전 조회 1,257
12년 전 조회 961
12년 전 조회 3,480
12년 전 조회 877
12년 전 조회 3,788
12년 전 조회 1,459
12년 전 조회 1,754
12년 전 조회 2,368
12년 전 조회 2,429
12년 전 조회 689
12년 전 조회 760
12년 전 조회 1,411
12년 전 조회 730
12년 전 조회 919
12년 전 조회 6,678
12년 전 조회 667
12년 전 조회 951
12년 전 조회 600
12년 전 조회 643
12년 전 조회 902
12년 전 조회 850
12년 전 조회 1,838
12년 전 조회 1,831
12년 전 조회 1,474
12년 전 조회 5,345
12년 전 조회 1,616
12년 전 조회 1,712
12년 전 조회 1,062
12년 전 조회 2,082
12년 전 조회 1,474
12년 전 조회 3,145
12년 전 조회 2,661
12년 전 조회 2,005
12년 전 조회 2,483
12년 전 조회 1,966
12년 전 조회 1,728
12년 전 조회 1,147
12년 전 조회 1,252
12년 전 조회 5,653
12년 전 조회 2,710
12년 전 조회 3,183
12년 전 조회 1,837
12년 전 조회 1,561
12년 전 조회 915
12년 전 조회 1,926
12년 전 조회 1,036
12년 전 조회 1,605
12년 전 조회 2,870
12년 전 조회 2,441
12년 전 조회 1,783
12년 전 조회 6,762
12년 전 조회 2,598
12년 전 조회 815
12년 전 조회 771
12년 전 조회 1,506
12년 전 조회 2,166
12년 전 조회 984
12년 전 조회 807
12년 전 조회 1,437
12년 전 조회 1,577
12년 전 조회 798
12년 전 조회 1,235
12년 전 조회 1,511
12년 전 조회 892
12년 전 조회 1,182
12년 전 조회 3,759
12년 전 조회 1,525
12년 전 조회 1,055
12년 전 조회 954
12년 전 조회 1,315
12년 전 조회 1,554
12년 전 조회 4,341
12년 전 조회 5,673
12년 전 조회 803
12년 전 조회 2,372
12년 전 조회 6,840
12년 전 조회 2,500
12년 전 조회 9,436
12년 전 조회 632
12년 전 조회 827
12년 전 조회 1,461
12년 전 조회 1,765
12년 전 조회 1,426
12년 전 조회 2,034
🐛 버그신고