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

· 12년 전 · 2500 · 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,634
12년 전 조회 1,500
12년 전 조회 4,025
12년 전 조회 1,650
12년 전 조회 985
12년 전 조회 1,665
12년 전 조회 765
12년 전 조회 1,041
12년 전 조회 1,435
12년 전 조회 1,329
12년 전 조회 5,291
12년 전 조회 2,641
12년 전 조회 1,912
12년 전 조회 754
12년 전 조회 2,360
12년 전 조회 958
12년 전 조회 1,275
12년 전 조회 980
12년 전 조회 3,498
12년 전 조회 901
12년 전 조회 3,809
12년 전 조회 1,473
12년 전 조회 1,774
12년 전 조회 2,388
12년 전 조회 2,441
12년 전 조회 701
12년 전 조회 775
12년 전 조회 1,423
12년 전 조회 744
12년 전 조회 934
12년 전 조회 6,691
12년 전 조회 679
12년 전 조회 976
12년 전 조회 618
12년 전 조회 662
12년 전 조회 921
12년 전 조회 868
12년 전 조회 1,856
12년 전 조회 1,848
12년 전 조회 1,490
12년 전 조회 5,358
12년 전 조회 1,632
12년 전 조회 1,730
12년 전 조회 1,080
12년 전 조회 2,097
12년 전 조회 1,490
12년 전 조회 3,162
12년 전 조회 2,680
12년 전 조회 2,022
12년 전 조회 2,501
12년 전 조회 1,977
12년 전 조회 1,750
12년 전 조회 1,165
12년 전 조회 1,273
12년 전 조회 5,673
12년 전 조회 2,735
12년 전 조회 3,199
12년 전 조회 1,855
12년 전 조회 1,573
12년 전 조회 936
12년 전 조회 1,946
12년 전 조회 1,052
12년 전 조회 1,623
12년 전 조회 2,888
12년 전 조회 2,460
12년 전 조회 1,802
12년 전 조회 6,783
12년 전 조회 2,615
12년 전 조회 835
12년 전 조회 781
12년 전 조회 1,529
12년 전 조회 2,189
12년 전 조회 1,001
12년 전 조회 824
12년 전 조회 1,459
12년 전 조회 1,594
12년 전 조회 819
12년 전 조회 1,247
12년 전 조회 1,527
12년 전 조회 908
12년 전 조회 1,201
12년 전 조회 3,773
12년 전 조회 1,541
12년 전 조회 1,076
12년 전 조회 969
12년 전 조회 1,332
12년 전 조회 1,572
12년 전 조회 4,359
12년 전 조회 5,699
12년 전 조회 823
12년 전 조회 2,394
12년 전 조회 6,856
12년 전 조회 2,517
12년 전 조회 9,453
12년 전 조회 646
12년 전 조회 842
12년 전 조회 1,479
12년 전 조회 1,785
12년 전 조회 1,446
12년 전 조회 2,054
🐛 버그신고