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

· 12년 전 · 2604 · 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,750
12년 전 조회 1,603
12년 전 조회 4,137
12년 전 조회 1,770
12년 전 조회 1,088
12년 전 조회 1,782
12년 전 조회 881
12년 전 조회 1,162
12년 전 조회 1,544
12년 전 조회 1,432
12년 전 조회 5,393
12년 전 조회 2,746
12년 전 조회 2,018
12년 전 조회 858
12년 전 조회 2,450
12년 전 조회 1,059
12년 전 조회 1,368
12년 전 조회 1,079
12년 전 조회 3,594
12년 전 조회 1,000
12년 전 조회 3,911
12년 전 조회 1,593
12년 전 조회 1,869
12년 전 조회 2,477
12년 전 조회 2,546
12년 전 조회 801
12년 전 조회 886
12년 전 조회 1,539
12년 전 조회 860
12년 전 조회 1,038
12년 전 조회 6,793
12년 전 조회 790
12년 전 조회 1,089
12년 전 조회 738
12년 전 조회 761
12년 전 조회 1,020
12년 전 조회 959
12년 전 조회 1,970
12년 전 조회 1,961
12년 전 조회 1,592
12년 전 조회 5,474
12년 전 조회 1,743
12년 전 조회 1,834
12년 전 조회 1,190
12년 전 조회 2,196
12년 전 조회 1,602
12년 전 조회 3,250
12년 전 조회 2,776
12년 전 조회 2,135
12년 전 조회 2,605
12년 전 조회 2,086
12년 전 조회 1,861
12년 전 조회 1,259
12년 전 조회 1,371
12년 전 조회 5,766
12년 전 조회 2,837
12년 전 조회 3,315
12년 전 조회 1,955
12년 전 조회 1,671
12년 전 조회 1,039
12년 전 조회 2,045
12년 전 조회 1,169
12년 전 조회 1,741
12년 전 조회 2,993
12년 전 조회 2,570
12년 전 조회 1,904
12년 전 조회 6,876
12년 전 조회 2,710
12년 전 조회 945
12년 전 조회 896
12년 전 조회 1,633
12년 전 조회 2,279
12년 전 조회 1,113
12년 전 조회 937
12년 전 조회 1,566
12년 전 조회 1,690
12년 전 조회 926
12년 전 조회 1,332
12년 전 조회 1,625
12년 전 조회 1,008
12년 전 조회 1,292
12년 전 조회 3,902
12년 전 조회 1,650
12년 전 조회 1,175
12년 전 조회 1,068
12년 전 조회 1,448
12년 전 조회 1,679
12년 전 조회 4,453
12년 전 조회 5,800
12년 전 조회 924
12년 전 조회 2,491
12년 전 조회 6,955
12년 전 조회 2,617
12년 전 조회 9,568
12년 전 조회 752
12년 전 조회 957
12년 전 조회 1,577
12년 전 조회 1,899
12년 전 조회 1,546
12년 전 조회 2,177