简体   繁体   English

使用PHP使用SPAN包装锚标签内容

[英]Wrap anchor tag content with SPAN using PHP

I need to take some simple UL tag generated in PHP (Joomla 1.5) and wrap each anchor tag's text content with a SPAN tag. 我需要使用一些在PHP(Joomla 1.5)中生成的简单UL标记,并用SPAN标记包装每个锚标记的文本内容。 The incoming HTML looks like this: 传入的HTML如下所示:

<ul>
  <li>
      <a href="#">Home</a>
    </li>
    <li>
      <a href="#">Watch UNC-TV</a>
    </li>
    <li>
      <a href="#" >Contact</a>
    </li>
</ul>

The output needs to look like this: 输出需要如下所示:

<ul id="top-nav" class="flatList">
  <li class="selected">
      <a href="#"><span class="embed embed-top-nav">Home</span>
        <p >news, highlights</p></a>
    </li>
    <li>
      <a href="#"><span class="embed embed-top-nav">Watch UNC-TV</span>
        <p>schedule, local programs</p></a>
    </li>
    <li id="nav-last">
      <a href="#"><span class="embed embed-top-nav">Contact</span>
        <p>feedback, connect, share</p></a>
    </li>
</ul>

Also note the class added to the active LI tag ("selected") and the class added to the last one in the list ("nav-last"). 还要注意添加到活动LI标签的类(“选定”)和添加到列表中最后一个类的类(“ nav-last”)。 This is in Joomla 1.5, where I'm overriding the mod_mainmenu module used by the Main Menu. 这是在Joomla 1.5中,在这里我覆盖了Main Menu中使用的mod_mainmenu模块。 The code uses the SimpleXML library to read and write the HTML in the modMainMenuXMLCallback(&$node, $args) : 该代码使用SimpleXML库在modMainMenuXMLCallback(&$node, $args)读写HTML:

<?php
defined('_JEXEC') or die('Restricted access');

if ( ! defined('fancyMenuPatch') ) 
{
  function fancyMenuPatch($result,$tag){
    // Replace UL tag with ours. 
    // Replace LI tag with ours.

    // Add to the start of the UL tag.
    $begin_ul = "<ul id=\"top-nav\" class=\"flatList\">";
    $begin_li = "<li>"; //not sure what to do with this.

    // do the replacement
    $result = str_replace("<ul>",$begin_ul, $result);
    $result = str_replace("<li>", $begin_li, $result);

    return $result;
  }

  define('fancyMenuPatch', true);
}

if ( ! defined('modMainMenuXMLCallbackDefined') )
{
function modMainMenuXMLCallback(&$node, $args)
{
  $user = &JFactory::getUser();
  $menu = &JSite::getMenu();
  $active   = $menu->getActive();
  $path = isset($active) ? array_reverse($active->tree) : null;

  if (($args['end']) && ($node->attributes('level') >= $args['end']))
  {
    $children = $node->children();
    foreach ($node->children() as $child)
    {
      if ($child->name() == 'ul') {
        $node->removeChild($child);
      }
    }
  }

  if ($node->name() == 'ul') {
    foreach ($node->children() as $child)
    {
      if ($child->attributes('access') > $user->get('aid', 0)) {
        $node->removeChild($child);
      }
    }
  }

  if (($node->name() == 'li') && isset($node->ul)) {
    $node->addAttribute('class', 'parent');
  }

  if (isset($path) && (in_array($node->attributes('id'), $path) || in_array($node->attributes('rel'), $path)))
  {
    if ($node->attributes('class')) {
      $node->addAttribute('class', $node->attributes('class').' active');
    } else {
      $node->addAttribute('class', 'active');
    }
  }
  else
  {
    if (isset($args['children']) && !$args['children'])
    {
      $children = $node->children();
      foreach ($node->children() as $child)
      {
        if ($child->name() == 'ul') {
          $node->removeChild($child);
        }
      }
    }
  }

  if (($node->name() == 'li') && ($id = $node->attributes('id'))) {
    if ($node->attributes('class')) {
      $node->addAttribute('class', $node->attributes('class').' item'.$id);
    } else {
      $node->addAttribute('class', 'item'.$id);
    }
  }

  if (isset($path) && $node->attributes('id') == $path[0]) {
    $node->addAttribute('id', 'current');
  } else {
    $node->removeAttribute('id');
  }
  $node->removeAttribute('rel');
  $node->removeAttribute('level');
  $node->removeAttribute('access');
}
  define('modMainMenuXMLCallbackDefined', true);
}

ob_start();

modMainMenuHelper::render($params, 'modMyMainMenuXMLCallback');
$menu_html = ob_get_contents();
ob_end_clean(); 

if($params->get('menutype')=="primarynav"){
  $tag = $params->get('tag_id');
  }
//output the menu!
echo fancyMenuPatch($menu_html,$tag);
?>

Thank you. 谢谢。

Ok so here is how you want to do this. 好的,这就是您要执行的操作。 Jump out of the default.php script you posted above. 跳出上面发布的default.php脚本。 As Joomla! 作为Joomla! uses the core mainmenu module for all menus and trees down from that you will need to edit the helper script. 对所有菜单和树使用核心mainmenu模块,从该菜单开始,您将需要编辑帮助程序脚本。 This should stick regardless of any 3rd party menu extension you may be using. 无论您正在使用任何第三方菜单扩展名,它都应保持不变。

rootdirectory/modules/mod_mainmenu/helper.php

Now jump down to the switch statement at line 358. Specifically we want to edit line 363. It looks like this: 现在跳到第358行的switch语句。特别是我们要编辑363行。它看起来像这样:

$data = '<a href="'.$tmp->url.'">'.$image.$tmp->name.'</a>';

Now edit it to be this: 现在将其编辑为:

$data = '<span class="embed embed-top-nav"><a href="'.$tmp->url.'">'.$image.$tmp->name.'</a></span>';

there you go, now menus utilized by the Joomla! 您去了,现在Joomla使用了菜单! getMenu class will add this span tag around the links. getMenu类将在链接周围添加此span标签。

Note that you may also want to do this to line 375 for links that will open in a new window et cetra. 请注意,对于可能在新窗口中打开的链接,您可能还想对第375行进行此操作。 Also note Louis' funny comment " // hrm...this is a bit dickey". 还要注意路易斯的有趣评论“ // hrm ... this is dicdic”。

cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM