2010-12-06 38 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);