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

· 12년 전 · 2389 · 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,522
12년 전 조회 1,372
12년 전 조회 3,904
12년 전 조회 1,521
12년 전 조회 862
12년 전 조회 1,559
12년 전 조회 648
12년 전 조회 920
12년 전 조회 1,317
12년 전 조회 1,200
12년 전 조회 5,175
12년 전 조회 2,530
12년 전 조회 1,790
12년 전 조회 638
12년 전 조회 2,239
12년 전 조회 835
12년 전 조회 1,149
12년 전 조회 868
12년 전 조회 3,373
12년 전 조회 787
12년 전 조회 3,682
12년 전 조회 1,368
12년 전 조회 1,650
12년 전 조회 2,265
12년 전 조회 2,327
12년 전 조회 584
12년 전 조회 658
12년 전 조회 1,318
12년 전 조회 628
12년 전 조회 811
12년 전 조회 6,592
12년 전 조회 570
12년 전 조회 858
12년 전 조회 501
12년 전 조회 539
12년 전 조회 805
12년 전 조회 743
12년 전 조회 1,742
12년 전 조회 1,735
12년 전 조회 1,377
12년 전 조회 5,245
12년 전 조회 1,517
12년 전 조회 1,615
12년 전 조회 965
12년 전 조회 1,982
12년 전 조회 1,381
12년 전 조회 3,047
12년 전 조회 2,554
12년 전 조회 1,912
12년 전 조회 2,390
12년 전 조회 1,867
12년 전 조회 1,638
12년 전 조회 1,037
12년 전 조회 1,147
12년 전 조회 5,567
12년 전 조회 2,616
12년 전 조회 3,094
12년 전 조회 1,748
12년 전 조회 1,465
12년 전 조회 817
12년 전 조회 1,827
12년 전 조회 934
12년 전 조회 1,512
12년 전 조회 2,777
12년 전 조회 2,346
12년 전 조회 1,686
12년 전 조회 6,671
12년 전 조회 2,500
12년 전 조회 717
12년 전 조회 673
12년 전 조회 1,422
12년 전 조회 2,066
12년 전 조회 905
12년 전 조회 711
12년 전 조회 1,344
12년 전 조회 1,479
12년 전 조회 712
12년 전 조회 1,136
12년 전 조회 1,407
12년 전 조회 800
12년 전 조회 1,085
12년 전 조회 3,681
12년 전 조회 1,430
12년 전 조회 966
12년 전 조회 860
12년 전 조회 1,222
12년 전 조회 1,457
12년 전 조회 4,247
12년 전 조회 5,577
12년 전 조회 709
12년 전 조회 2,278
12년 전 조회 6,743
12년 전 조회 2,417
12년 전 조회 9,337
12년 전 조회 544
12년 전 조회 746
12년 전 조회 1,362
12년 전 조회 1,677
12년 전 조회 1,335
12년 전 조회 1,944
🐛 버그신고