应用PHP代码后查找/替换
我正在尝试构建一个输出自定义JSON代码的PHP表单 看一看: 应用程序创建的JSON代码输出错误。我需要进行搜索并替换以删除一些逗号 (部分)源代码如下所示:应用PHP代码后查找/替换,php,javascript,replace,Php,Javascript,Replace,我正在尝试构建一个输出自定义JSON代码的PHP表单 看一看: 应用程序创建的JSON代码输出错误。我需要进行搜索并替换以删除一些逗号 (部分)源代码如下所示: { <!-- ALBUM ART --><span <?php if($artdisplay!="block") echo "style=\"display:none;\""; ?>>"art": { "x": <?php echo $artx; ?>, "y": <?php ech
{
<!-- 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>
}
这就是我试图使用的代码:
$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
)
);
这就像我需要一个子数组的样式。。。设置格式的正确方法是什么?您基本上没问题,但有一些语法错误:
$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\u 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);
你基本上没问题,但你有一些语法错误:
$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\u 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);
您手动编写json而不是使用
json\u encode
php函数有什么原因吗?出于好奇,为什么不将数据存储在数组中,然后简单地json\u encode()
结束它?您手动编写json而不是使用json\u encode
php函数有什么原因吗?只是出于好奇,为什么不将数据存储在一个数组中,然后简单地json\u encode()
。这个数组结构可以工作,但我需要添加一个子数组。请参阅我上面编辑的文章中的代码示例。您的更新方向正确。。。你的语法不正确。。。看我的更新。谢谢你。这个数组结构可以工作,但我需要添加一个子数组。请参阅我上面编辑的文章中的代码示例。您的更新方向正确。。。你的语法不正确。。。查看我的更新。