是否可以设置打印输出的样式


Is it possible to style a printf output?

我想设置一个printf行的样式。

printf("- %d ". $maand ." & %d " . $dag , $months, $days);

假设我希望这条线是红色的输出。我怎样才能做到这一点?我认为它需要像这样的东西。

printf("<font style="color:red;">- %d ". $maand ." & %d " . $dag , $months, $days"</font>");

Martincarlin87 的答案似乎有效,但是当我使用两个 printf 语句并希望像这样设置其中一个语句时,我无法使其工作:

<?(strtotime($date1) > strtotime($date2)) ? `printf("%d " . $maand . " & %d " . $dag , $months, $days) :  '<div class="red">'
    <?php printf("- %d ". $maand ." & %d " . $dag , $months, $days); ?>
'</div>'?>

HTML

<div class="red">
    <?php printf("- %d ". $maand ." & %d " . $dag , $months, $days); ?>
</div>

.CSS

.red {
    color: red;
}

font标记在 HTML5 中已弃用。

编辑

试试这个三元if

<?(strtotime($date1) > strtotime($date2)) ? printf("%d " . $maand . " & %d " . $dag , $months, $days) :  printf("<div class='"red'">- %d ". $maand ." & %d " . $dag . "</div>" , $months, $days) . '' ?>

这就是我的做法:)

.PHP:

<?php 
    $val1 = "Hello ";
    $val2 = "World!";
    printf('%s %s %s %s', $val1, '<span class="green">', $val2, '</span>');
?>

.CSS:

.green {
    color: #8AD17A; //green
}

输出:

"世界"更改为绿色的"你好世界"文本

不,没有办法。但是你可以使用 sprintf() 来返回字符串。

 echo '<font style="color:red;">'.sprintf("- %d ". $maand ." & %d " . $dag , $months, $days).'</font>';