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

· 12년 전 · 2423 · 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,555
12년 전 조회 1,412
12년 전 조회 3,941
12년 전 조회 1,569
12년 전 조회 899
12년 전 조회 1,603
12년 전 조회 683
12년 전 조회 959
12년 전 조회 1,353
12년 전 조회 1,252
12년 전 조회 5,217
12년 전 조회 2,567
12년 전 조회 1,833
12년 전 조회 667
12년 전 조회 2,285
12년 전 조회 873
12년 전 조회 1,191
12년 전 조회 908
12년 전 조회 3,411
12년 전 조회 821
12년 전 조회 3,726
12년 전 조회 1,396
12년 전 조회 1,683
12년 전 조회 2,300
12년 전 조회 2,360
12년 전 조회 633
12년 전 조회 695
12년 전 조회 1,350
12년 전 조회 671
12년 전 조회 849
12년 전 조회 6,628
12년 전 조회 599
12년 전 조회 886
12년 전 조회 537
12년 전 조회 575
12년 전 조회 844
12년 전 조회 782
12년 전 조회 1,773
12년 전 조회 1,769
12년 전 조회 1,407
12년 전 조회 5,280
12년 전 조회 1,552
12년 전 조회 1,651
12년 전 조회 995
12년 전 조회 2,008
12년 전 조회 1,410
12년 전 조회 3,084
12년 전 조회 2,594
12년 전 조회 1,943
12년 전 조회 2,423
12년 전 조회 1,893
12년 전 조회 1,671
12년 전 조회 1,071
12년 전 조회 1,182
12년 전 조회 5,598
12년 전 조회 2,652
12년 전 조회 3,125
12년 전 조회 1,785
12년 전 조회 1,499
12년 전 조회 852
12년 전 조회 1,865
12년 전 조회 971
12년 전 조회 1,544
12년 전 조회 2,813
12년 전 조회 2,376
12년 전 조회 1,722
12년 전 조회 6,702
12년 전 조회 2,536
12년 전 조회 743
12년 전 조회 703
12년 전 조회 1,447
12년 전 조회 2,092
12년 전 조회 932
12년 전 조회 735
12년 전 조회 1,381
12년 전 조회 1,513
12년 전 조회 737
12년 전 조회 1,165
12년 전 조회 1,439
12년 전 조회 831
12년 전 조회 1,120
12년 전 조회 3,708
12년 전 조회 1,456
12년 전 조회 992
12년 전 조회 886
12년 전 조회 1,256
12년 전 조회 1,489
12년 전 조회 4,281
12년 전 조회 5,608
12년 전 조회 744
12년 전 조회 2,305
12년 전 조회 6,781
12년 전 조회 2,445
12년 전 조회 9,375
12년 전 조회 572
12년 전 조회 772
12년 전 조회 1,394
12년 전 조회 1,700
12년 전 조회 1,361
12년 전 조회 1,967
🐛 버그신고