需要一个正则表达式来添加css类到第一个和最后一个列表项 [英] Need a regex to add css class to first and last list item

查看:159
本文介绍了需要一个正则表达式来添加css类到第一个和最后一个列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:
谢谢大家的意见。一些额外的信息。



这只是一小块标记(20行)我正在使用,并旨在利用正则表达式来做这项工作。 / p>

我也有能力破解脚本(电子商务),在导航构建时插入类。我想限制我已经到位的数量,当我去更新到最新版本的软件时,让自己保持简单。



这样说,我很清楚我的情况和我可用的各种选项。我的正则表达式的第一部分按预期工作。我发现真的或多或少,看看有人会说,嘿,这是很容易只是改变这个.....



在接近几个我的努力,这是更多的原则在这一点上。只是知道(和学习)这个问题的解决方案存在。我也很讨厌被一段代码殴打。



ORIGINAL:



表达式将CSS类添加到有序列表中的第一个和最后一个列表项。我尝试了一堆不同的方式,但不能产生我想要的结果。



我有一个正则表达式的第一个列表项,但似乎没有一个正确的一个出来的最后一个。这是我的工作:

  $ patterns = array('/< ul +([^< )< li / m','/>(< li> /> / s'); 
$ replace = array('< ul $ 1< li class =first','< li class =last$ 2 $ 3< / ul>');
$ navigation = preg_replace($ patterns,$ replace,$ navigation);

任何帮助将非常感激。

解决方案

Jamie Zawinski 会有有关这个的话 ...



你有一个合适的HTML解析器吗?我不知道是否有像 hpricot 可用于PHP的东西,但这是正确的方式处理用它。你至少可以使用hpricot为你做第一次清理。



如果你实际上是生成HTML - 在那里。看起来你想生成一些导航,并有一个 .first .last 它的东西。请退一步尝试。


UPDATE: Thank you all for your input. Some additional information.

It's really just a small chunk of markup (20 lines) I'm working with and had aimed to to leverage a regex to do the work.

I also do have the ability to hack up the script (an ecommerce one) to insert the classes as the navigation is built. I wanted to limit the number of hacks I have in place to keep things easier on myself when I go to update to the latest version of the software.

With that said, I'm pretty aware of my situation and the various options available to me. The first part of my regex works as expected. I posted really more or less to see if someone would say, "hey dummy, this is easy just change this....."

After coming close with a few of my efforts, it's more of the principle at this point. To just know (and learn) a solution exists for this problem. I also hate being beaten by a piece of code.

ORIGINAL:

I'm trying to leverage regular expressions to add a CSS a class to the first and last list items within an ordered list. I've tried a bunch of different ways but can't produce the results I'm looking for.

I've got a regular expression for the first list item but can't seem to figure a correct one out for the last. Here is what I'm working with:

    $patterns = array('/<ul+([^<]*)<li/m', '/<([^<]*)(?<=<li)(.*)<\/ul>/s');
    $replace = array('<ul$1<li class="first"','<li class="last"$2$3</ul>');
    $navigation = preg_replace($patterns, $replace, $navigation); 

Any help would be greatly appreciated.

解决方案

Jamie Zawinski would have something to say about this...

Do you have a proper HTML parser? I don't know if there's anything like hpricot available for PHP, but that's the right way to deal with it. You could at least employ hpricot to do the first cleanup for you.

If you're actually generating the HTML -- do it there. It looks like you want to generate some navigation and have a .first and .last kind of thing on it. Take a step back and try that.

这篇关于需要一个正则表达式来添加css类到第一个和最后一个列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆