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

· 12년 전 · 2511 · 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,645
12년 전 조회 1,513
12년 전 조회 4,036
12년 전 조회 1,660
12년 전 조회 995
12년 전 조회 1,676
12년 전 조회 781
12년 전 조회 1,057
12년 전 조회 1,452
12년 전 조회 1,339
12년 전 조회 5,300
12년 전 조회 2,649
12년 전 조회 1,919
12년 전 조회 768
12년 전 조회 2,366
12년 전 조회 969
12년 전 조회 1,284
12년 전 조회 994
12년 전 조회 3,507
12년 전 조회 907
12년 전 조회 3,818
12년 전 조회 1,486
12년 전 조회 1,781
12년 전 조회 2,396
12년 전 조회 2,457
12년 전 조회 711
12년 전 조회 787
12년 전 조회 1,438
12년 전 조회 758
12년 전 조회 945
12년 전 조회 6,699
12년 전 조회 693
12년 전 조회 994
12년 전 조회 632
12년 전 조회 668
12년 전 조회 930
12년 전 조회 875
12년 전 조회 1,870
12년 전 조회 1,859
12년 전 조회 1,502
12년 전 조회 5,364
12년 전 조회 1,641
12년 전 조회 1,742
12년 전 조회 1,092
12년 전 조회 2,106
12년 전 조회 1,511
12년 전 조회 3,167
12년 전 조회 2,693
12년 전 조회 2,036
12년 전 조회 2,512
12년 전 조회 1,994
12년 전 조회 1,765
12년 전 조회 1,176
12년 전 조회 1,282
12년 전 조회 5,688
12년 전 조회 2,739
12년 전 조회 3,211
12년 전 조회 1,871
12년 전 조회 1,587
12년 전 조회 947
12년 전 조회 1,960
12년 전 조회 1,066
12년 전 조회 1,635
12년 전 조회 2,893
12년 전 조회 2,466
12년 전 조회 1,813
12년 전 조회 6,793
12년 전 조회 2,627
12년 전 조회 843
12년 전 조회 793
12년 전 조회 1,543
12년 전 조회 2,201
12년 전 조회 1,014
12년 전 조회 833
12년 전 조회 1,469
12년 전 조회 1,603
12년 전 조회 835
12년 전 조회 1,259
12년 전 조회 1,535
12년 전 조회 923
12년 전 조회 1,216
12년 전 조회 3,788
12년 전 조회 1,558
12년 전 조회 1,087
12년 전 조회 977
12년 전 조회 1,347
12년 전 조회 1,589
12년 전 조회 4,374
12년 전 조회 5,707
12년 전 조회 832
12년 전 조회 2,403
12년 전 조회 6,871
12년 전 조회 2,529
12년 전 조회 9,465
12년 전 조회 664
12년 전 조회 856
12년 전 조회 1,487
12년 전 조회 1,800
12년 전 조회 1,459
12년 전 조회 2,066
🐛 버그신고