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

· 12년 전 · 3792 · 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,973
12년 전 조회 2,784
12년 전 조회 5,383
12년 전 조회 3,014
12년 전 조회 2,288
12년 전 조회 2,976
12년 전 조회 2,096
12년 전 조회 2,321
12년 전 조회 2,735
12년 전 조회 2,626
12년 전 조회 6,614
12년 전 조회 3,973
12년 전 조회 3,235
12년 전 조회 2,022
12년 전 조회 3,553
12년 전 조회 2,201
12년 전 조회 2,513
12년 전 조회 2,258
12년 전 조회 4,786
12년 전 조회 2,144
12년 전 조회 5,021
12년 전 조회 2,732
12년 전 조회 3,004
12년 전 조회 3,618
12년 전 조회 3,697
12년 전 조회 1,937
12년 전 조회 2,033
12년 전 조회 2,689
12년 전 조회 2,007
12년 전 조회 2,155
12년 전 조회 7,950
12년 전 조회 1,951
12년 전 조회 2,223
12년 전 조회 1,879
12년 전 조회 1,910
12년 전 조회 2,165
12년 전 조회 2,141
12년 전 조회 3,111
12년 전 조회 3,133
12년 전 조회 2,718
12년 전 조회 6,614
12년 전 조회 2,897
12년 전 조회 2,980
12년 전 조회 2,366
12년 전 조회 3,365
12년 전 조회 2,770
12년 전 조회 4,456
12년 전 조회 3,953
12년 전 조회 3,308
12년 전 조회 3,793
12년 전 조회 3,244
12년 전 조회 3,053
12년 전 조회 2,404
12년 전 조회 2,539
12년 전 조회 6,977
12년 전 조회 4,033
12년 전 조회 4,494
12년 전 조회 3,132
12년 전 조회 2,837
12년 전 조회 2,228
12년 전 조회 3,216
12년 전 조회 2,335
12년 전 조회 2,857
12년 전 조회 4,111
12년 전 조회 3,724
12년 전 조회 3,071
12년 전 조회 8,020
12년 전 조회 3,881
12년 전 조회 2,080
12년 전 조회 2,047
12년 전 조회 2,782
12년 전 조회 3,407
12년 전 조회 2,239
12년 전 조회 2,142
12년 전 조회 2,754
12년 전 조회 2,923
12년 전 조회 2,122
12년 전 조회 2,554
12년 전 조회 2,853
12년 전 조회 2,212
12년 전 조회 2,558
12년 전 조회 5,154
12년 전 조회 2,872
12년 전 조회 2,388
12년 전 조회 2,273
12년 전 조회 2,696
12년 전 조회 2,932
12년 전 조회 5,720
12년 전 조회 7,045
12년 전 조회 2,181
12년 전 조회 3,627
12년 전 조회 8,213
12년 전 조회 3,875
12년 전 조회 1.1만
12년 전 조회 1,998
12년 전 조회 2,181
12년 전 조회 2,858
12년 전 조회 3,167
12년 전 조회 2,805
12년 전 조회 3,317