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

· 12년 전 · 2563 · 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,693
12년 전 조회 1,548
12년 전 조회 4,075
12년 전 조회 1,705
12년 전 조회 1,040
12년 전 조회 1,718
12년 전 조회 831
12년 전 조회 1,104
12년 전 조회 1,499
12년 전 조회 1,386
12년 전 조회 5,342
12년 전 조회 2,693
12년 전 조회 1,969
12년 전 조회 811
12년 전 조회 2,399
12년 전 조회 1,006
12년 전 조회 1,319
12년 전 조회 1,037
12년 전 조회 3,543
12년 전 조회 951
12년 전 조회 3,861
12년 전 조회 1,534
12년 전 조회 1,814
12년 전 조회 2,431
12년 전 조회 2,498
12년 전 조회 756
12년 전 조회 830
12년 전 조회 1,488
12년 전 조회 804
12년 전 조회 989
12년 전 조회 6,752
12년 전 조회 740
12년 전 조회 1,042
12년 전 조회 682
12년 전 조회 714
12년 전 조회 968
12년 전 조회 917
12년 전 조회 1,912
12년 전 조회 1,907
12년 전 조회 1,546
12년 전 조회 5,416
12년 전 조회 1,692
12년 전 조회 1,784
12년 전 조회 1,138
12년 전 조회 2,153
12년 전 조회 1,555
12년 전 조회 3,205
12년 전 조회 2,725
12년 전 조회 2,085
12년 전 조회 2,564
12년 전 조회 2,030
12년 전 조회 1,808
12년 전 조회 1,213
12년 전 조회 1,324
12년 전 조회 5,725
12년 전 조회 2,786
12년 전 조회 3,261
12년 전 조회 1,910
12년 전 조회 1,628
12년 전 조회 992
12년 전 조회 1,997
12년 전 조회 1,113
12년 전 조회 1,689
12년 전 조회 2,946
12년 전 조회 2,525
12년 전 조회 1,850
12년 전 조회 6,832
12년 전 조회 2,665
12년 전 조회 894
12년 전 조회 844
12년 전 조회 1,579
12년 전 조회 2,238
12년 전 조회 1,058
12년 전 조회 885
12년 전 조회 1,513
12년 전 조회 1,648
12년 전 조회 880
12년 전 조회 1,295
12년 전 조회 1,581
12년 전 조회 974
12년 전 조회 1,258
12년 전 조회 3,850
12년 전 조회 1,594
12년 전 조회 1,137
12년 전 조회 1,026
12년 전 조회 1,398
12년 전 조회 1,628
12년 전 조회 4,407
12년 전 조회 5,756
12년 전 조회 879
12년 전 조회 2,440
12년 전 조회 6,904
12년 전 조회 2,566
12년 전 조회 9,513
12년 전 조회 715
12년 전 조회 910
12년 전 조회 1,532
12년 전 조회 1,851
12년 전 조회 1,504
12년 전 조회 2,125
🐛 버그신고