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

· 12년 전 · 2567 · 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,700
12년 전 조회 1,553
12년 전 조회 4,081
12년 전 조회 1,709
12년 전 조회 1,044
12년 전 조회 1,721
12년 전 조회 839
12년 전 조회 1,108
12년 전 조회 1,503
12년 전 조회 1,390
12년 전 조회 5,343
12년 전 조회 2,696
12년 전 조회 1,974
12년 전 조회 814
12년 전 조회 2,404
12년 전 조회 1,015
12년 전 조회 1,320
12년 전 조회 1,043
12년 전 조회 3,545
12년 전 조회 951
12년 전 조회 3,862
12년 전 조회 1,538
12년 전 조회 1,822
12년 전 조회 2,435
12년 전 조회 2,501
12년 전 조회 758
12년 전 조회 830
12년 전 조회 1,493
12년 전 조회 810
12년 전 조회 992
12년 전 조회 6,754
12년 전 조회 747
12년 전 조회 1,047
12년 전 조회 692
12년 전 조회 716
12년 전 조회 968
12년 전 조회 922
12년 전 조회 1,917
12년 전 조회 1,911
12년 전 조회 1,547
12년 전 조회 5,419
12년 전 조회 1,696
12년 전 조회 1,786
12년 전 조회 1,145
12년 전 조회 2,156
12년 전 조회 1,564
12년 전 조회 3,210
12년 전 조회 2,727
12년 전 조회 2,089
12년 전 조회 2,568
12년 전 조회 2,036
12년 전 조회 1,811
12년 전 조회 1,217
12년 전 조회 1,327
12년 전 조회 5,727
12년 전 조회 2,791
12년 전 조회 3,269
12년 전 조회 1,911
12년 전 조회 1,631
12년 전 조회 997
12년 전 조회 2,003
12년 전 조회 1,115
12년 전 조회 1,694
12년 전 조회 2,950
12년 전 조회 2,535
12년 전 조회 1,854
12년 전 조회 6,834
12년 전 조회 2,668
12년 전 조회 899
12년 전 조회 848
12년 전 조회 1,585
12년 전 조회 2,238
12년 전 조회 1,063
12년 전 조회 893
12년 전 조회 1,515
12년 전 조회 1,653
12년 전 조회 882
12년 전 조회 1,297
12년 전 조회 1,581
12년 전 조회 977
12년 전 조회 1,260
12년 전 조회 3,859
12년 전 조회 1,602
12년 전 조회 1,138
12년 전 조회 1,029
12년 전 조회 1,399
12년 전 조회 1,632
12년 전 조회 4,410
12년 전 조회 5,759
12년 전 조회 881
12년 전 조회 2,447
12년 전 조회 6,908
12년 전 조회 2,569
12년 전 조회 9,516
12년 전 조회 719
12년 전 조회 916
12년 전 조회 1,536
12년 전 조회 1,854
12년 전 조회 1,509
12년 전 조회 2,130
🐛 버그신고