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

· 12년 전 · 2447 · 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,576
12년 전 조회 1,437
12년 전 조회 3,967
12년 전 조회 1,599
12년 전 조회 933
12년 전 조회 1,615
12년 전 조회 709
12년 전 조회 985
12년 전 조회 1,371
12년 전 조회 1,274
12년 전 조회 5,233
12년 전 조회 2,584
12년 전 조회 1,858
12년 전 조회 697
12년 전 조회 2,311
12년 전 조회 902
12년 전 조회 1,216
12년 전 조회 926
12년 전 조회 3,438
12년 전 조회 845
12년 전 조회 3,750
12년 전 조회 1,417
12년 전 조회 1,710
12년 전 조회 2,322
12년 전 조회 2,387
12년 전 조회 652
12년 전 조회 719
12년 전 조회 1,375
12년 전 조회 691
12년 전 조회 872
12년 전 조회 6,640
12년 전 조회 622
12년 전 조회 915
12년 전 조회 563
12년 전 조회 601
12년 전 조회 867
12년 전 조회 800
12년 전 조회 1,802
12년 전 조회 1,789
12년 전 조회 1,426
12년 전 조회 5,305
12년 전 조회 1,570
12년 전 조회 1,671
12년 전 조회 1,017
12년 전 조회 2,035
12년 전 조회 1,438
12년 전 조회 3,105
12년 전 조회 2,622
12년 전 조회 1,962
12년 전 조회 2,448
12년 전 조회 1,911
12년 전 조회 1,692
12년 전 조회 1,096
12년 전 조회 1,207
12년 전 조회 5,618
12년 전 조회 2,681
12년 전 조회 3,145
12년 전 조회 1,798
12년 전 조회 1,519
12년 전 조회 873
12년 전 조회 1,891
12년 전 조회 986
12년 전 조회 1,567
12년 전 조회 2,836
12년 전 조회 2,399
12년 전 조회 1,740
12년 전 조회 6,724
12년 전 조회 2,561
12년 전 조회 770
12년 전 조회 723
12년 전 조회 1,467
12년 전 조회 2,121
12년 전 조회 949
12년 전 조회 763
12년 전 조회 1,399
12년 전 조회 1,537
12년 전 조회 764
12년 전 조회 1,189
12년 전 조회 1,462
12년 전 조회 858
12년 전 조회 1,145
12년 전 조회 3,726
12년 전 조회 1,484
12년 전 조회 1,012
12년 전 조회 912
12년 전 조회 1,274
12년 전 조회 1,508
12년 전 조회 4,305
12년 전 조회 5,631
12년 전 조회 763
12년 전 조회 2,326
12년 전 조회 6,802
12년 전 조회 2,464
12년 전 조회 9,400
12년 전 조회 591
12년 전 조회 794
12년 전 조회 1,420
12년 전 조회 1,724
12년 전 조회 1,388
12년 전 조회 1,992
🐛 버그신고