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

· 12년 전 · 2643 · 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,779
12년 전 조회 1,633
12년 전 조회 4,164
12년 전 조회 1,798
12년 전 조회 1,120
12년 전 조회 1,802
12년 전 조회 911
12년 전 조회 1,185
12년 전 조회 1,577
12년 전 조회 1,460
12년 전 조회 5,422
12년 전 조회 2,771
12년 전 조회 2,046
12년 전 조회 890
12년 전 조회 2,475
12년 전 조회 1,089
12년 전 조회 1,398
12년 전 조회 1,112
12년 전 조회 3,615
12년 전 조회 1,031
12년 전 조회 3,936
12년 전 조회 1,616
12년 전 조회 1,902
12년 전 조회 2,505
12년 전 조회 2,577
12년 전 조회 838
12년 전 조회 909
12년 전 조회 1,572
12년 전 조회 885
12년 전 조회 1,065
12년 전 조회 6,821
12년 전 조회 820
12년 전 조회 1,119
12년 전 조회 764
12년 전 조회 793
12년 전 조회 1,047
12년 전 조회 989
12년 전 조회 1,996
12년 전 조회 1,986
12년 전 조회 1,623
12년 전 조회 5,495
12년 전 조회 1,779
12년 전 조회 1,868
12년 전 조회 1,223
12년 전 조회 2,227
12년 전 조회 1,632
12년 전 조회 3,273
12년 전 조회 2,802
12년 전 조회 2,169
12년 전 조회 2,644
12년 전 조회 2,115
12년 전 조회 1,893
12년 전 조회 1,294
12년 전 조회 1,398
12년 전 조회 5,796
12년 전 조회 2,864
12년 전 조회 3,347
12년 전 조회 1,980
12년 전 조회 1,704
12년 전 조회 1,074
12년 전 조회 2,078
12년 전 조회 1,197
12년 전 조회 1,768
12년 전 조회 3,021
12년 전 조회 2,602
12년 전 조회 1,937
12년 전 조회 6,903
12년 전 조회 2,737
12년 전 조회 983
12년 전 조회 933
12년 전 조회 1,673
12년 전 조회 2,317
12년 전 조회 1,138
12년 전 조회 975
12년 전 조회 1,602
12년 전 조회 1,732
12년 전 조회 961
12년 전 조회 1,371
12년 전 조회 1,664
12년 전 조회 1,048
12년 전 조회 1,335
12년 전 조회 3,936
12년 전 조회 1,677
12년 전 조회 1,204
12년 전 조회 1,101
12년 전 조회 1,475
12년 전 조회 1,719
12년 전 조회 4,493
12년 전 조회 5,826
12년 전 조회 959
12년 전 조회 2,522
12년 전 조회 6,979
12년 전 조회 2,645
12년 전 조회 9,595
12년 전 조회 787
12년 전 조회 991
12년 전 조회 1,608
12년 전 조회 1,930
12년 전 조회 1,574
12년 전 조회 2,209