2010-12-06 22 views
0

我試圖構建一個輸出自定義JSON代碼的PHP表單。PHP代碼已被應用後查找/替換

看看:http://s194239704.onlinehome.us/bcembed/該應用程序創建是錯誤的

的JSON編碼輸出。我需要做一個搜索並替換掉一些逗號。

(部分)的源代碼如下所示:

{ 
<!-- ALBUM ART --><span <?php if($artdisplay!="block") echo "style=\"display:none;\""; ?>>"art": { "x": <?php echo $artx; ?>, "y": <?php echo $arty; ?>, "w": <?php if($artsize=="small") {echo "100";} elseif($artsize=="large") {echo "150";} ?>, "h": <?php if($artsize=="small") {echo "100";} elseif($artsize=="large") {echo "150";} ?>, "show": true },</span> 
<!-- MAINTEXT --><span <?php if($maintextdisplay!="block") echo "style=\"display:none;\""; ?>>"maintext": { "x": <?php echo $maintextx; ?>, "y": <?php echo $maintexty; ?>, "w": <?php echo $maintextw; ?>, "h": <?php echo $maintexth; ?>, "show": true, "styles": { "fontSize": "<?php echo $maintextfontsize; ?>", "textAlign": "<?php echo $maintextalign; ?>", <?php if($maintextbold=="bold") echo "\"fontWeight\": \"" . $maintextbold . "\","; ?> <?php if($maintextitalic=="italic") echo "\"fontStyle\": \"" . $maintextitalic . "\","; ?> }},</span> 
} 

我想運行搜索/應用PHP後更換。我試圖用JavaScript搜索/替換包裝整個東西,因爲我認爲PHP會在Javascript代碼之前運行。但沒有發生。

你能告訴我在我的頭上嗎?半稱職的複製和粘貼只能讓我那麼遠,


編輯:我不知道json_encode。它似乎在工作,但我遇到了另一個障礙。我想有這樣的輸出:

"currenttime": { 
    "x": 0, 
    "y": 0, 
    "w": 30, 
    "h": 30, 
    "show": true, 
    "styles": { 
    "fontSize": "13", 
    "fontWeight": "bold", 
    "fontStyle": "null", 
    "textAlign": "center" 
    } 
} 

這是我想要使用的代碼:

$jsonData['currenttime'] = array(
    'x' => $currenttimex, 
    'y' => $currenttimey, 
    'w' => $currenttimew, 
    'h' => $currenttimeh, 
    'show' => $currenttimedisplay=="block" ? true : false, 
    ['styles'] = array(
    'fontSize' => $currenttimefontsize, 
    'fontWeight' => $currenttimebold, 
    'fontStyle' => $currenttimeitalic, 
    'textAlign' => $currenttimealign 
) 
); 

這就像我需要一個子陣列的風格...什麼正確的方式來格式化?

+2

是否有一個原因您選擇手動編寫json而不是使用`json_encode` php函數? – prodigitalson 2010-12-06 21:27:53

+0

出於好奇,爲什麼不把數據存儲在一個數組中,而只是簡單地將`json_encode()`結尾? – 2010-12-06 21:29:55

回答

4

特殊照顧基本上是好的,但你有一些語法錯誤:

$jsonData['currenttime'] = array(
    'x' => $currenttimex, 
    'y' => $currenttimey, 
    'w' => $currenttimew, 
    'h' => $currenttimeh, 
    'show' => $currenttimedisplay =="block" ? true : false, 
    'styles' => array(
    'fontSize' => $currenttimefontsize, 
    'fontWeight' => $currenttimebold, 
    'fontStyle' => $currenttimeitalic, 
    'textAlign' => $currenttimealign 
) 
); 

我手動與此難道不...使用json_encode代替:

$jsonData = array(); 

$jsonData['art'] = array(
    'x' => $artx, 
    'y' => $arty, 
    'w' => $artsize=="small" ? 100 : ($artsize == 'large' ? 150 : null), 
    'h' => $artsize=="small" ? 100 : ($artsize == 'large' ? 150 : null), 
    'show' => true 
); 

echo json_encode($jsonData);