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

· 12년 전 · 2448 · 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,439
12년 전 조회 3,968
12년 전 조회 1,600
12년 전 조회 933
12년 전 조회 1,615
12년 전 조회 709
12년 전 조회 987
12년 전 조회 1,372
12년 전 조회 1,275
12년 전 조회 5,234
12년 전 조회 2,587
12년 전 조회 1,858
12년 전 조회 697
12년 전 조회 2,312
12년 전 조회 903
12년 전 조회 1,217
12년 전 조회 928
12년 전 조회 3,440
12년 전 조회 848
12년 전 조회 3,750
12년 전 조회 1,418
12년 전 조회 1,712
12년 전 조회 2,324
12년 전 조회 2,389
12년 전 조회 653
12년 전 조회 721
12년 전 조회 1,375
12년 전 조회 691
12년 전 조회 872
12년 전 조회 6,642
12년 전 조회 625
12년 전 조회 915
12년 전 조회 565
12년 전 조회 601
12년 전 조회 869
12년 전 조회 800
12년 전 조회 1,803
12년 전 조회 1,790
12년 전 조회 1,426
12년 전 조회 5,305
12년 전 조회 1,570
12년 전 조회 1,672
12년 전 조회 1,019
12년 전 조회 2,036
12년 전 조회 1,438
12년 전 조회 3,106
12년 전 조회 2,624
12년 전 조회 1,963
12년 전 조회 2,449
12년 전 조회 1,913
12년 전 조회 1,692
12년 전 조회 1,096
12년 전 조회 1,209
12년 전 조회 5,618
12년 전 조회 2,682
12년 전 조회 3,149
12년 전 조회 1,801
12년 전 조회 1,520
12년 전 조회 875
12년 전 조회 1,893
12년 전 조회 986
12년 전 조회 1,568
12년 전 조회 2,838
12년 전 조회 2,399
12년 전 조회 1,740
12년 전 조회 6,726
12년 전 조회 2,563
12년 전 조회 771
12년 전 조회 726
12년 전 조회 1,467
12년 전 조회 2,123
12년 전 조회 952
12년 전 조회 764
12년 전 조회 1,401
12년 전 조회 1,537
12년 전 조회 765
12년 전 조회 1,189
12년 전 조회 1,463
12년 전 조회 860
12년 전 조회 1,146
12년 전 조회 3,729
12년 전 조회 1,486
12년 전 조회 1,015
12년 전 조회 913
12년 전 조회 1,275
12년 전 조회 1,508
12년 전 조회 4,305
12년 전 조회 5,633
12년 전 조회 763
12년 전 조회 2,326
12년 전 조회 6,803
12년 전 조회 2,464
12년 전 조회 9,402
12년 전 조회 592
12년 전 조회 795
12년 전 조회 1,422
12년 전 조회 1,724
12년 전 조회 1,389
12년 전 조회 1,992
🐛 버그신고