How to format font style and color in echo

2019-04-28 19:56发布

问题:

I have a small snippet of code that I want to style from 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;

Any php assistance would be helpful.

回答1:

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>";
    }
}


回答2:

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.

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 style attribute; <span> is the simplest element -- it doesn't have any formatting of its own, it just serves as a place to attach attributes.

Another popular way to write it is with string concatenation:

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


回答3:

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>";


回答4:

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


回答5:

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


回答6:

You should also use the style 'color' and not '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.