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

· 12년 전 · 2498 · 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,631
12년 전 조회 1,498
12년 전 조회 4,023
12년 전 조회 1,649
12년 전 조회 980
12년 전 조회 1,664
12년 전 조회 761
12년 전 조회 1,037
12년 전 조회 1,431
12년 전 조회 1,326
12년 전 조회 5,290
12년 전 조회 2,637
12년 전 조회 1,908
12년 전 조회 749
12년 전 조회 2,360
12년 전 조회 958
12년 전 조회 1,275
12년 전 조회 975
12년 전 조회 3,495
12년 전 조회 894
12년 전 조회 3,803
12년 전 조회 1,468
12년 전 조회 1,771
12년 전 조회 2,379
12년 전 조회 2,441
12년 전 조회 700
12년 전 조회 773
12년 전 조회 1,418
12년 전 조회 740
12년 전 조회 930
12년 전 조회 6,689
12년 전 조회 674
12년 전 조회 971
12년 전 조회 615
12년 전 조회 659
12년 전 조회 916
12년 전 조회 866
12년 전 조회 1,853
12년 전 조회 1,845
12년 전 조회 1,486
12년 전 조회 5,355
12년 전 조회 1,630
12년 전 조회 1,728
12년 전 조회 1,074
12년 전 조회 2,096
12년 전 조회 1,489
12년 전 조회 3,160
12년 전 조회 2,676
12년 전 조회 2,018
12년 전 조회 2,499
12년 전 조회 1,972
12년 전 조회 1,744
12년 전 조회 1,160
12년 전 조회 1,270
12년 전 조회 5,666
12년 전 조회 2,732
12년 전 조회 3,195
12년 전 조회 1,851
12년 전 조회 1,569
12년 전 조회 934
12년 전 조회 1,943
12년 전 조회 1,049
12년 전 조회 1,620
12년 전 조회 2,885
12년 전 조회 2,458
12년 전 조회 1,799
12년 전 조회 6,778
12년 전 조회 2,613
12년 전 조회 829
12년 전 조회 781
12년 전 조회 1,525
12년 전 조회 2,185
12년 전 조회 999
12년 전 조회 822
12년 전 조회 1,453
12년 전 조회 1,587
12년 전 조회 815
12년 전 조회 1,246
12년 전 조회 1,525
12년 전 조회 906
12년 전 조회 1,197
12년 전 조회 3,771
12년 전 조회 1,538
12년 전 조회 1,070
12년 전 조회 968
12년 전 조회 1,331
12년 전 조회 1,570
12년 전 조회 4,355
12년 전 조회 5,696
12년 전 조회 819
12년 전 조회 2,391
12년 전 조회 6,852
12년 전 조회 2,516
12년 전 조회 9,451
12년 전 조회 644
12년 전 조회 840
12년 전 조회 1,475
12년 전 조회 1,778
12년 전 조회 1,442
12년 전 조회 2,049
🐛 버그신고