如何在public函数中添加html和css?

How to add html and css in public function?

这是我的代码:

  public function getName() {
      return trim(stripslashes(strtr($this->_firstName . ' ' . $this->_surname1 . ' --ID is: ' . $this->_surname2, $this->_trans)));
  }

输出是这样的。示例:Alex Rodriguez --ID 是:A-Rod

如何将 Alex 字体更改为蓝色,Rodriguez 更改为红色,A-Rod黄色和 --ID 是: 绿色?

<span style="color:#0000CD;">Alex</span> <span style="color:#FF0000;">Rodriguez</span> <span style="color:#008000;">--ID is:</span> <span style="color:#FFFF00;">A-Rod</span>

如何在上面的代码中添加 css 或 html?

例如:如何将字体颜色更改为蓝色,或者如何使用样式和 html ...

非常感谢。

您可以做 2 件事。将 css 写在一个单独的文件中,然后 link 它到您的页面(或者只是将所有内容都放在 style 标签中)或者您可以为每个元素写 incline css (不是推荐的 )。您可能希望将文本输出到不同的元素中(例如 span),以便您可以轻松地对其进行操作。

您的代码可能类似于:

<html>
<head>
<style>
.first-name{
    color : blue;
}

.surname{
    color : red;
}

.person-id{
    color : green;
}
</style>
</head>
<body>
<?php
//Rest of your PHP code
public function getName()
  {
  return trim(stripslashes(strtr('<span class="first-name">'.$this->_firstName. '</span><span class="surname">' . $this->_surname1 . '</span> --ID is: <span class="person-id">' . $this->_surname2.'</span>', $this->_trans)));
  }
//Rest of your PHP code
 ?>
 </body>
 </html>

请记住 PHP 是一种服务器端语言,您的浏览器实际获得的结果是:

<html>
<head>
<style>
.first-name{
 color : blue;
}

.surname{
 color : red;
}

.person-id{
 color : green;
}
</style>
</head>
<body>
<span class="first-name">John</span><span class="surname">Smith</span> --ID is: <span class="person-id">John-S</span>
</body>
</html>