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

· 12년 전 · 3640 · 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,824
12년 전 조회 2,671
12년 전 조회 5,253
12년 전 조회 2,878
12년 전 조회 2,154
12년 전 조회 2,852
12년 전 조회 1,970
12년 전 조회 2,200
12년 전 조회 2,631
12년 전 조회 2,478
12년 전 조회 6,492
12년 전 조회 3,850
12년 전 조회 3,132
12년 전 조회 1,905
12년 전 조회 3,407
12년 전 조회 2,077
12년 전 조회 2,396
12년 전 조회 2,136
12년 전 조회 4,671
12년 전 조회 2,024
12년 전 조회 4,888
12년 전 조회 2,616
12년 전 조회 2,872
12년 전 조회 3,510
12년 전 조회 3,572
12년 전 조회 1,817
12년 전 조회 1,912
12년 전 조회 2,564
12년 전 조회 1,898
12년 전 조회 2,046
12년 전 조회 7,849
12년 전 조회 1,827
12년 전 조회 2,103
12년 전 조회 1,764
12년 전 조회 1,792
12년 전 조회 2,041
12년 전 조회 2,020
12년 전 조회 2,976
12년 전 조회 3,009
12년 전 조회 2,609
12년 전 조회 6,477
12년 전 조회 2,774
12년 전 조회 2,862
12년 전 조회 2,256
12년 전 조회 3,250
12년 전 조회 2,652
12년 전 조회 4,326
12년 전 조회 3,822
12년 전 조회 3,165
12년 전 조회 3,641
12년 전 조회 3,088
12년 전 조회 2,905
12년 전 조회 2,275
12년 전 조회 2,396
12년 전 조회 6,835
12년 전 조회 3,889
12년 전 조회 4,365
12년 전 조회 2,997
12년 전 조회 2,723
12년 전 조회 2,089
12년 전 조회 3,075
12년 전 조회 2,217
12년 전 조회 2,727
12년 전 조회 3,960
12년 전 조회 3,583
12년 전 조회 2,928
12년 전 조회 7,865
12년 전 조회 3,730
12년 전 조회 1,940
12년 전 조회 1,912
12년 전 조회 2,637
12년 전 조회 3,262
12년 전 조회 2,111
12년 전 조회 2,004
12년 전 조회 2,617
12년 전 조회 2,792
12년 전 조회 1,978
12년 전 조회 2,409
12년 전 조회 2,718
12년 전 조회 2,074
12년 전 조회 2,411
12년 전 조회 4,995
12년 전 조회 2,754
12년 전 조회 2,268
12년 전 조회 2,145
12년 전 조회 2,560
12년 전 조회 2,800
12년 전 조회 5,593
12년 전 조회 6,917
12년 전 조회 2,055
12년 전 조회 3,553
12년 전 조회 8,069
12년 전 조회 3,733
12년 전 조회 1.1만
12년 전 조회 1,875
12년 전 조회 2,060
12년 전 조회 2,750
12년 전 조회 3,057
12년 전 조회 2,676
12년 전 조회 3,236