php - 将类添加到此行... php/css

标签 php css

我在 php 中有这一行

<a title="'.htmlspecialchars($User->Name).'" href="'.$Href.'"'.$LinkClass.'>

我需要添加另一个名为 tip 的类

上面的代码生成如下内容:

<a class="ProfileLink" href="/respond/profile/2/422" title="422">

如您所见,$LinkClass 为我提供了很棒的“ProfileLink”类

但我需要像“ProfileLink tip”这样解析类

所以不确定如何将上面的 $LinkClass 修改为类似 $LinkClass 的提示

这可能太基础了,我只是见树不见林

//

编辑:添加

最终的 html 输出需要是:

<a class="ProfileLink tip" href="/respond/profile/2/422" title="422">

//

添加:

此函数的完整 php 输出是:

  if (!function_exists('UserPhoto')) {
   function UserPhoto($User, $Options = array()) {
    $User = (object)$User;
  if (is_string($Options))
     $Options = array('LinkClass' => $Options);

  $LinkClass = GetValue('LinkClass', $Options, 'ProfileLink');
  $ImgClass = GetValue('ImageClass', $Options, 'ProfilePhotoMedium');

  $LinkClass = $LinkClass == '' ? '' : ' class="'.$LinkClass.'"';

  $Photo = $User->Photo;
  if (!$Photo && function_exists('UserPhotoDefaultUrl'))
     $Photo = UserPhotoDefaultUrl($User);

  if ($Photo) {
     if (!preg_match('`^https?://`i', $Photo)) {
        $PhotoUrl = Gdn_Upload::Url(ChangeBasename($Photo, 'n%s'));
     } else {
        $PhotoUrl = $Photo;
     }
     $Href = Url(UserUrl($User));
     return '<a title="'.htmlspecialchars($User->Name).'" href="'.$Href.'"'.$LinkClass.'>'
        .Img($PhotoUrl, array('alt' => htmlspecialchars($User->Name), 'class' => $ImgClass))
        .'</a>';
  } else {
     return '';
  }

}

最佳答案

如何为类属性使用数组?像这样:

$LinkClass= array();
$LinkClassVal = GetValue('LinkClass', $Options, 'ProfileLink'); 

if($LinkClassVal){
    $LinkClass[] = $LinkClassVal;
}

$LinkClass[] = "tip";

然后在返回时:

return '<a title="'.htmlspecialchars($User->Name).'" href="'.$Href.'"'.implode(" ",$LinkClass).'>'
    .Img($PhotoUrl, array('alt' => htmlspecialchars($User->Name), 'class' => $ImgClass))
    .'</a>';

关于php - 将类添加到此行... php/css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10263509/

相关文章:

css - 为什么向 css 添加线条边框会扰乱其正确的位移?

css - 使用媒体查询从纵向切换到横向时出现问题

html - iPad 上的 iFrame 无法在内部呈现全部内容

html - 为什么我的表没有行?

php - 重写规则不允许我访问我域的 index.php

php - 在 PHP 中,如何将关联数组展平为只有值的数组?

php - 发布表单后有条件地设置 JavaScript 函数

javascript - 将代码从 JavaScript 转换为 PHP

html - 为什么 png 图像不在父 div 中居中?

php - 在 where 子句中使用小于和大于语句进行查询