如何在PHP中的数组中添加html?

时间:2012-06-10 20:53:12

标签: php arrays drupal

我想在每个标签打印为数组元素之前添加一些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);
 }
}

1 个答案:

答案 0 :(得分:2)

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
  )
));