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

· 12년 전 · 2554 · 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,689
12년 전 조회 1,544
12년 전 조회 4,071
12년 전 조회 1,694
12년 전 조회 1,033
12년 전 조회 1,715
12년 전 조회 826
12년 전 조회 1,098
12년 전 조회 1,494
12년 전 조회 1,382
12년 전 조회 5,340
12년 전 조회 2,689
12년 전 조회 1,964
12년 전 조회 806
12년 전 조회 2,395
12년 전 조회 1,005
12년 전 조회 1,313
12년 전 조회 1,032
12년 전 조회 3,539
12년 전 조회 947
12년 전 조회 3,857
12년 전 조회 1,532
12년 전 조회 1,811
12년 전 조회 2,423
12년 전 조회 2,496
12년 전 조회 752
12년 전 조회 822
12년 전 조회 1,484
12년 전 조회 793
12년 전 조회 982
12년 전 조회 6,746
12년 전 조회 734
12년 전 조회 1,035
12년 전 조회 675
12년 전 조회 709
12년 전 조회 960
12년 전 조회 912
12년 전 조회 1,908
12년 전 조회 1,901
12년 전 조회 1,541
12년 전 조회 5,412
12년 전 조회 1,684
12년 전 조회 1,780
12년 전 조회 1,131
12년 전 조회 2,148
12년 전 조회 1,548
12년 전 조회 3,202
12년 전 조회 2,719
12년 전 조회 2,077
12년 전 조회 2,555
12년 전 조회 2,024
12년 전 조회 1,801
12년 전 조회 1,204
12년 전 조회 1,317
12년 전 조회 5,724
12년 전 조회 2,780
12년 전 조회 3,254
12년 전 조회 1,906
12년 전 조회 1,624
12년 전 조회 985
12년 전 조회 1,991
12년 전 조회 1,107
12년 전 조회 1,681
12년 전 조회 2,937
12년 전 조회 2,520
12년 전 조회 1,846
12년 전 조회 6,827
12년 전 조회 2,662
12년 전 조회 884
12년 전 조회 835
12년 전 조회 1,574
12년 전 조회 2,237
12년 전 조회 1,054
12년 전 조회 877
12년 전 조회 1,510
12년 전 조회 1,644
12년 전 조회 876
12년 전 조회 1,289
12년 전 조회 1,572
12년 전 조회 966
12년 전 조회 1,253
12년 전 조회 3,841
12년 전 조회 1,590
12년 전 조회 1,130
12년 전 조회 1,018
12년 전 조회 1,391
12년 전 조회 1,622
12년 전 조회 4,403
12년 전 조회 5,747
12년 전 조회 873
12년 전 조회 2,438
12년 전 조회 6,898
12년 전 조회 2,561
12년 전 조회 9,508
12년 전 조회 709
12년 전 조회 901
12년 전 조회 1,526
12년 전 조회 1,842
12년 전 조회 1,501
12년 전 조회 2,114
🐛 버그신고