我写了一个小脚本,每当某个值设置为 0 时,项目都应该加粗。 但我有一种感觉,我在重复自己...有没有办法避免这种重复?
$output .= '<td>';
if ('0' == $value['treated']) {
$output .= '<b>';
}
$output .= $value['from'];
$output .= substr($value['message'], 20);
if ('0' == $value['treated']) {
$output .= '</b>';
}
$output .= '</td>';
你可以设置一个类吗?
$output .= '<td' . ('0' == $value['treated'] ? ' class="bold"' : '') . '>';
$output .= $value['from'];
$output .= substr($value['message'], 20);
$output .= '</td>';
然后,您需要做的就是添加
.bold { font-weight: bold; }
到您的 CSS 文件
$output = $value['from'] . substr($value['message'], 20);
if ('0' == $value['treated']) {
$output = "<b>$output</b>";
}
$output = "<td>$output</td>";
我不知道
这是否是您要查找的内容,但您可以缓冲内容:
$output .= "<td>";
$buffer = $value['from'];
$buffer = substr($value['message'], 20);
$output .= ('0' == $value['treated'])
? "<b>" . $buffer . "</b>"
: $buffer;
编辑:我看到骗子是第一个:)
这是您可以实现的简单方法
$output .= '<td>';
$bs = '';
$be = '';
if ('0' == $value['treated']) {
$bs = '<b>';
$be = '</b>';
}
$output .= $bs;
$output .= $value['from'];
$output .= substr($value['message'], 20);
$output .= $be;
$output .= '</td>';
或
$output .= '<td';
if ('0' == $value['treated']) {
$output .=' style="font-weight:bold;"';
}
$output .= '>';
$output .= $value['from'];
$output .= substr($value['message'], 20);
$output .= '</td>';
$dom = new DOMDocument;
$dom->loadHTML('<html/>');
$body = $dom->documentElement->appendChild($dom->createElement('body'));
for ( $i = 0; $i < 100; ++$i ) {
$div = $body->appendChild($dom->createElement('div'));
if ( rand() % 2 ) {
$div->setAttribute('class', 'highlighted');
$div->nodeValue = 'bold';
} else {
$div->nodeValue = 'normal';
}
}
die($dom->saveHTML());