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

· 12년 전 · 2544 · 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,685
12년 전 조회 1,538
12년 전 조회 4,063
12년 전 조회 1,687
12년 전 조회 1,027
12년 전 조회 1,710
12년 전 조회 818
12년 전 조회 1,092
12년 전 조회 1,486
12년 전 조회 1,374
12년 전 조회 5,334
12년 전 조회 2,685
12년 전 조회 1,958
12년 전 조회 799
12년 전 조회 2,393
12년 전 조회 1,000
12년 전 조회 1,310
12년 전 조회 1,025
12년 전 조회 3,534
12년 전 조회 946
12년 전 조회 3,851
12년 전 조회 1,525
12년 전 조회 1,804
12년 전 조회 2,416
12년 전 조회 2,489
12년 전 조회 745
12년 전 조회 815
12년 전 조회 1,478
12년 전 조회 788
12년 전 조회 972
12년 전 조회 6,744
12년 전 조회 728
12년 전 조회 1,027
12년 전 조회 667
12년 전 조회 703
12년 전 조회 959
12년 전 조회 904
12년 전 조회 1,903
12년 전 조회 1,893
12년 전 조회 1,535
12년 전 조회 5,403
12년 전 조회 1,678
12년 전 조회 1,775
12년 전 조회 1,128
12년 전 조회 2,142
12년 전 조회 1,544
12년 전 조회 3,200
12년 전 조회 2,716
12년 전 조회 2,071
12년 전 조회 2,545
12년 전 조회 2,017
12년 전 조회 1,795
12년 전 조회 1,197
12년 전 조회 1,313
12년 전 조회 5,719
12년 전 조회 2,774
12년 전 조회 3,246
12년 전 조회 1,902
12년 전 조회 1,621
12년 전 조회 983
12년 전 조회 1,990
12년 전 조회 1,106
12년 전 조회 1,670
12년 전 조회 2,928
12년 전 조회 2,515
12년 전 조회 1,843
12년 전 조회 6,822
12년 전 조회 2,659
12년 전 조회 879
12년 전 조회 829
12년 전 조회 1,570
12년 전 조회 2,233
12년 전 조회 1,047
12년 전 조회 872
12년 전 조회 1,505
12년 전 조회 1,636
12년 전 조회 865
12년 전 조회 1,284
12년 전 조회 1,568
12년 전 조회 954
12년 전 조회 1,245
12년 전 조회 3,831
12년 전 조회 1,586
12년 전 조회 1,126
12년 전 조회 1,008
12년 전 조회 1,384
12년 전 조회 1,611
12년 전 조회 4,401
12년 전 조회 5,736
12년 전 조회 866
12년 전 조회 2,435
12년 전 조회 6,891
12년 전 조회 2,560
12년 전 조회 9,499
12년 전 조회 700
12년 전 조회 895
12년 전 조회 1,523
12년 전 조회 1,837
12년 전 조회 1,493
12년 전 조회 2,105
🐛 버그신고