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

· 12년 전 · 2392 · 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,526
12년 전 조회 1,375
12년 전 조회 3,908
12년 전 조회 1,528
12년 전 조회 867
12년 전 조회 1,561
12년 전 조회 655
12년 전 조회 927
12년 전 조회 1,319
12년 전 조회 1,207
12년 전 조회 5,178
12년 전 조회 2,536
12년 전 조회 1,793
12년 전 조회 643
12년 전 조회 2,244
12년 전 조회 841
12년 전 조회 1,153
12년 전 조회 873
12년 전 조회 3,376
12년 전 조회 793
12년 전 조회 3,683
12년 전 조회 1,372
12년 전 조회 1,654
12년 전 조회 2,268
12년 전 조회 2,329
12년 전 조회 587
12년 전 조회 666
12년 전 조회 1,321
12년 전 조회 637
12년 전 조회 816
12년 전 조회 6,597
12년 전 조회 574
12년 전 조회 863
12년 전 조회 503
12년 전 조회 544
12년 전 조회 807
12년 전 조회 748
12년 전 조회 1,748
12년 전 조회 1,738
12년 전 조회 1,379
12년 전 조회 5,248
12년 전 조회 1,521
12년 전 조회 1,621
12년 전 조회 971
12년 전 조회 1,986
12년 전 조회 1,388
12년 전 조회 3,049
12년 전 조회 2,558
12년 전 조회 1,917
12년 전 조회 2,393
12년 전 조회 1,872
12년 전 조회 1,642
12년 전 조회 1,042
12년 전 조회 1,153
12년 전 조회 5,572
12년 전 조회 2,620
12년 전 조회 3,101
12년 전 조회 1,752
12년 전 조회 1,468
12년 전 조회 824
12년 전 조회 1,830
12년 전 조회 938
12년 전 조회 1,516
12년 전 조회 2,782
12년 전 조회 2,349
12년 전 조회 1,691
12년 전 조회 6,674
12년 전 조회 2,500
12년 전 조회 723
12년 전 조회 676
12년 전 조회 1,426
12년 전 조회 2,072
12년 전 조회 907
12년 전 조회 719
12년 전 조회 1,350
12년 전 조회 1,483
12년 전 조회 713
12년 전 조회 1,138
12년 전 조회 1,410
12년 전 조회 801
12년 전 조회 1,090
12년 전 조회 3,684
12년 전 조회 1,434
12년 전 조회 971
12년 전 조회 863
12년 전 조회 1,224
12년 전 조회 1,461
12년 전 조회 4,250
12년 전 조회 5,582
12년 전 조회 714
12년 전 조회 2,281
12년 전 조회 6,746
12년 전 조회 2,420
12년 전 조회 9,341
12년 전 조회 548
12년 전 조회 749
12년 전 조회 1,365
12년 전 조회 1,681
12년 전 조회 1,337
12년 전 조회 1,947
🐛 버그신고