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

· 12년 전 · 3778 · 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년 전 조회 2,955
12년 전 조회 2,771
12년 전 조회 5,364
12년 전 조회 3,000
12년 전 조회 2,275
12년 전 조회 2,965
12년 전 조회 2,081
12년 전 조회 2,311
12년 전 조회 2,721
12년 전 조회 2,608
12년 전 조회 6,605
12년 전 조회 3,963
12년 전 조회 3,228
12년 전 조회 2,012
12년 전 조회 3,540
12년 전 조회 2,187
12년 전 조회 2,502
12년 전 조회 2,247
12년 전 조회 4,775
12년 전 조회 2,135
12년 전 조회 5,013
12년 전 조회 2,721
12년 전 조회 2,995
12년 전 조회 3,606
12년 전 조회 3,685
12년 전 조회 1,926
12년 전 조회 2,024
12년 전 조회 2,674
12년 전 조회 2,001
12년 전 조회 2,143
12년 전 조회 7,939
12년 전 조회 1,936
12년 전 조회 2,213
12년 전 조회 1,869
12년 전 조회 1,898
12년 전 조회 2,150
12년 전 조회 2,126
12년 전 조회 3,100
12년 전 조회 3,126
12년 전 조회 2,712
12년 전 조회 6,606
12년 전 조회 2,885
12년 전 조회 2,969
12년 전 조회 2,359
12년 전 조회 3,349
12년 전 조회 2,755
12년 전 조회 4,442
12년 전 조회 3,946
12년 전 조회 3,293
12년 전 조회 3,779
12년 전 조회 3,226
12년 전 조회 3,043
12년 전 조회 2,385
12년 전 조회 2,523
12년 전 조회 6,963
12년 전 조회 4,015
12년 전 조회 4,483
12년 전 조회 3,117
12년 전 조회 2,830
12년 전 조회 2,216
12년 전 조회 3,205
12년 전 조회 2,326
12년 전 조회 2,844
12년 전 조회 4,101
12년 전 조회 3,716
12년 전 조회 3,060
12년 전 조회 8,008
12년 전 조회 3,865
12년 전 조회 2,064
12년 전 조회 2,038
12년 전 조회 2,770
12년 전 조회 3,396
12년 전 조회 2,231
12년 전 조회 2,131
12년 전 조회 2,738
12년 전 조회 2,914
12년 전 조회 2,105
12년 전 조회 2,544
12년 전 조회 2,838
12년 전 조회 2,200
12년 전 조회 2,539
12년 전 조회 5,134
12년 전 조회 2,855
12년 전 조회 2,376
12년 전 조회 2,265
12년 전 조회 2,681
12년 전 조회 2,919
12년 전 조회 5,708
12년 전 조회 7,034
12년 전 조회 2,172
12년 전 조회 3,619
12년 전 조회 8,198
12년 전 조회 3,862
12년 전 조회 1.1만
12년 전 조회 1,986
12년 전 조회 2,173
12년 전 조회 2,852
12년 전 조회 3,156
12년 전 조회 2,784
12년 전 조회 3,305