如何在 php 中将 html 添加到数组中


How to add html to an array in php?

我正在尝试在每个打印为数组元素的标签之前添加一些html代码。  
我的代码:

  $term_links = array();
  foreach ($vars['node']->taxonomy as $term) 
  {
    $term_links[] = l($term->name, 'taxonomy/term/' . $term->tid,
      array(
        'attributes' => array(
          'title' => $term->description
    )));
  }
  $vars['node_terms'] = implode(', ', $term_links);

目前,标签由逗号分隔。我想在每个标签元素之前添加一个小图像img src="tag.png"该怎么做?

编辑 - 我当前的代码仍然无法正常工作。

 if (module_exists('taxonomy')) {
$img  = 'some html';
$text = $img . $term->name;
$path = 'taxonomy/term/' . $term->tid;

$term_links = array();
foreach ($vars['node']->taxonomy as $term) {

  $term_links[] = l($text, $path, array(
    'html' => TRUE,
      'attributes' => array(
        'title' => $term->description
    )));
 }
   $vars['node_terms'] = implode(', ', $term_links);
 }
}

Dupal的l()函数有一个选项"html",您可以将其设置为TRUE并使用IMG + TITLE作为标题。

下面是示例:

$img  = '<img src="..." />';
$text = $img . $term->name;
$path = 'taxonomy/term/' . $term->tid;
$term_links[] = l($text, $path, array(
  'html'       => TRUE,
  'attributes' => array(
    'title' => $term->description
  )
));