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

· 12년 전 · 2517 · 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,650
12년 전 조회 1,514
12년 전 조회 4,039
12년 전 조회 1,666
12년 전 조회 997
12년 전 조회 1,682
12년 전 조회 788
12년 전 조회 1,062
12년 전 조회 1,459
12년 전 조회 1,343
12년 전 조회 5,305
12년 전 조회 2,653
12년 전 조회 1,926
12년 전 조회 773
12년 전 조회 2,369
12년 전 조회 977
12년 전 조회 1,289
12년 전 조회 999
12년 전 조회 3,517
12년 전 조회 911
12년 전 조회 3,823
12년 전 조회 1,494
12년 전 조회 1,784
12년 전 조회 2,399
12년 전 조회 2,460
12년 전 조회 717
12년 전 조회 794
12년 전 조회 1,440
12년 전 조회 765
12년 전 조회 948
12년 전 조회 6,703
12년 전 조회 698
12년 전 조회 998
12년 전 조회 640
12년 전 조회 676
12년 전 조회 931
12년 전 조회 877
12년 전 조회 1,873
12년 전 조회 1,862
12년 전 조회 1,504
12년 전 조회 5,370
12년 전 조회 1,645
12년 전 조회 1,746
12년 전 조회 1,098
12년 전 조회 2,108
12년 전 조회 1,518
12년 전 조회 3,170
12년 전 조회 2,695
12년 전 조회 2,044
12년 전 조회 2,518
12년 전 조회 1,996
12년 전 조회 1,772
12년 전 조회 1,180
12년 전 조회 1,284
12년 전 조회 5,693
12년 전 조회 2,747
12년 전 조회 3,220
12년 전 조회 1,873
12년 전 조회 1,590
12년 전 조회 951
12년 전 조회 1,964
12년 전 조회 1,074
12년 전 조회 1,640
12년 전 조회 2,899
12년 전 조회 2,468
12년 전 조회 1,819
12년 전 조회 6,798
12년 전 조회 2,630
12년 전 조회 846
12년 전 조회 796
12년 전 조회 1,546
12년 전 조회 2,208
12년 전 조회 1,018
12년 전 조회 838
12년 전 조회 1,473
12년 전 조회 1,605
12년 전 조회 840
12년 전 조회 1,262
12년 전 조회 1,541
12년 전 조회 928
12년 전 조회 1,221
12년 전 조회 3,795
12년 전 조회 1,565
12년 전 조회 1,093
12년 전 조회 981
12년 전 조회 1,356
12년 전 조회 1,596
12년 전 조회 4,381
12년 전 조회 5,712
12년 전 조회 833
12년 전 조회 2,409
12년 전 조회 6,874
12년 전 조회 2,537
12년 전 조회 9,471
12년 전 조회 671
12년 전 조회 863
12년 전 조회 1,490
12년 전 조회 1,804
12년 전 조회 1,467
12년 전 조회 2,069
🐛 버그신고