php 如何在 echo 中格式化字体样式和颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16902152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:52:11  来源:igfitidea点击:

How to format font style and color in echo

phpfontsformatecho

提问by ValleyDigital

I have a small snippet of code that I want to style from echo.

我有一小段代码,我想从 echo 中设置样式。

foreach($months as $key => $month){
  if(strpos($filename,$month)!==false){
        echo '<style = "font-color: #ff0000"> Movie List for {$key} 2013 </style>';
    }
}

This is not working, and I've been looking over some resources to try to implement this. Basically I want font-family: Arial and font-size: 11px; and the font-color: #ff0000;

这不起作用,我一直在寻找一些资源来尝试实现这一点。基本上我想要 font-family: Arial 和 font-size: 11px; 和字体颜色:#ff0000;

Anyphp assistance would be helpful.

任何php 帮助都会有所帮助。

回答by Mahesh

foreach($months as $key => $month){
  if(strpos($filename,$month)!==false){
        echo "<div style ='font:11px/21px Arial,tahoma,sans-serif;color:#ff0000'> Movie List for $key 2013</div>";
    }
}

回答by Barmar

echo "<span style = 'font-color: #ff0000'> Movie List for {$key} 2013 </span>";

Variables are only expanded inside double quotes, not single quotes. Since the above uses double quotes for the PHP string, I switched to single quotes for the embedded HTML, to avoid having to escape the quotes.

变量只在双引号内展开,而不是单引号。由于以上对 PHP 字符串使用双引号,因此我为嵌入的 HTML 切换到单引号,以避免对引号进行转义。

The other problem with your code is that <style>tags are for entering CSS blocks, not for styling individual elements. To style an element, you need an element tag with a styleattribute; <span>is the simplest element -- it doesn't have any formatting of its own, it just serves as a place to attach attributes.

您的代码的另一个问题是<style>标签用于输入 CSS 块,而不是用于设置单个元素的样式。要为元素设置样式,您需要一个带有style属性的元素标签;<span>是最简单的元素——它没有任何自己的格式,它只是作为附加属性的地方。

Another popular way to write it is with string concatenation:

另一种流行的写法是字符串连接:

echo '<span style = "font-color: #ff0000"> Movie List for ' . $key . ' 2013 </span>';

回答by Julie Beck

Are you trying to echo out a style or an inline style? An inline style would be like

你是想呼应一种风格还是一种内联风格?内联样式就像

echo "<p style=\"font-color: #ff0000;\">text here</p>";

回答by dsharew

echo '< span style = "font-color: #ff0000"> Movie List for {$key} 2013 </span>';

回答by Srikanth Kolli

 echo "<a href='#' style = \"font-color: #ff0000;\"> Movie List for {$key} 2013 </a>";

回答by Micah

You should also use the style 'color' and not 'font-color'

您还应该使用样式 'color' 而不是 'font-color'

<?php

foreach($months as $key => $month){
  if(strpos($filename,$month)!==false){
        echo "<style = 'color: #ff0000;'> Movie List for {$key} 2013 </style>";
    }
}

?>

In general, the comments on double and single quotes are correct in other suggestions. $Variables only execute in double quotes.

一般来说,双引号和单引号的注释在其他建议中都是正确的。$Variables 只在双引号中执行。