我在 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/