简体   繁体   English

带有PHP和/或CSS的字体大小

[英]Font Size with php and/or CSS

I am trying to produce an effect where I have a title on a page all in Capitals. 我正在尝试产生一种效果,使我在大写字母的页面上都有标题。 I would however like to see the first character of each word with a slightly larger font size. 但是,我希望看到每个单词的第一个字符的字体都更大。

For example: 例如:

MY TITLE 我的标题

The M and T would be in size 16 font and the rest of the word would be in size 12. I am pulling the titles from a database (MySQL) so "MY TITLE" is just an example. M和T的字体大小为16,其余单词的大小为12。我从数据库(MySQL)中提取标题,因此“ MY TITLE”只是一个示例。 It could be any wording. 可以是任何措辞。

I realise there is a way to do this using substr (in php) and search for where the spaces are at the end of each word. 我意识到有一种方法可以使用substr(在php中)并在每个单词的末尾搜索空格。 But this seems to me as though it will get a bit messy with coding. 但是在我看来,这似乎会使编码有些混乱。

Does anyone have a better suggestion? 有谁有更好的建议?

I think something like this is what you are looking for. 我认为您正在寻找类似这样的东西。

PHP 的PHP

<?php
$text = "My Title";
$newText = preg_replace('/(\w)(\w+\b)/', '<span class="firstLetter">$1</span>$2', $text);

echo $newText;  // <span class="firstLetter">M</span>y <span class="firstLetter">T</span>itle

CSS 的CSS

.firstLetter{font-size: 125%;}

Use the CSS first letter pseudo-element by wrapping each first letter in a <span> tag: 通过将每个首字母包装在<span>标记中,使用CSS首字母伪元素

p span:first-letter
{ 
    font-size: 16pt;
}

Pure PHP solution: 纯PHP解决方案:

<?php
$str = '<h2><big>S</big>ome <big>t</big>ext</h2>';
echo strtoupper($str);
?>

CSS Solution: CSS解决方案:

<h2><span class="larger">S</span>ome <span class="larger">T</span>ext</h2>
<style type="text/css">
h2{text-transform:uppercase;}
.larger{font-size:larger;}
</style>

ADDITIONAL METHOD: If you want to be able to output a particular format with a variable number of words, you can do something like this: 其他方法:如果您希望能够以可变数量的单词输出特定格式,则可以执行以下操作:

<?php
$string = 'VariaBle Number oF WORds!';
$words = explode(' ', $string);
$modified = array();
foreach($words as $word)
{
    $modified[] = '<big>'.substr($word, 0, 1).'</big>'.substr($word, 1, strlen($word));
}
echo strtoupper(implode(' ', $modified));
?>

简单的CSS属性,

font-variant:small-caps;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM