【问题标题】:Regular expression to get particular class name from a tag in php正则表达式从 php 中的标记中获取特定的类名
【发布时间】:2013-02-20 09:15:56
【问题描述】:

我想使用正则表达式从匹配类名“coremetrics”的字符串/内容中获取标签

In Content href 将像这样<a href="/children" class="hp2s_desc_btn hp_sp13_btn_2 coremetrics">

我需要使用标识符类名称“coremetrics”(黑框)在数组中获取 a 标签(红框)

【问题讨论】:

  • 你能提供更多细节吗?
  • 你要什么标签。
  • a href 标签。 @基本桥
  • 我需要使用标识符类名称“coremetrics”@Nicholas Pickering 获取 a href 标记
  • 看看这个QueryPath soluion。它允许搜索 CSS 选择器。

标签: php regex joomla2.5


【解决方案1】:

你可以试试这个正则表达式 <a(.+)?class="(.+)?coremetrics"> 演示链接http://regex101.com/r/xQ6dR8

第二个正则表达式<a.*?href="([^"]*)".*?class=".*?coremetrics.*?"[^>]*>

如果你使用这个 php 代码

preg_match_all('/<a.*?href="([^"]*)".*?class=".*?coremetrics.*?"[^>]*>/', '<a href="yoursite.com" class="hp2s_desc_btn hp_sp13_btn_2 coremetrics">', $matches);

print_r($matches);

你得到这样的arry

Array
(
    [0] => Array
        (
            [0] => <a href="yoursite.com" class="hp2s_desc_btn hp_sp13_btn_2 coremetrics">
        )

    [1] => Array
        (
            [0] => yoursite.com
        )

)

使用echo $matches[1][0]; 回显 href 值

【讨论】:

  • im using $link ='/]*>/s'; 但我需要再添加一个检查点类名称“coremetrics”。你能帮我编辑这个 reg 表达式吗@Basic Bridge
  • @Ashlash 这可能会有所帮助&lt;a.*?href="([^"]*)".*?class=".*?coremetrics.*?"[^&gt;]*&gt;
【解决方案2】:

如果您需要检索属于“coremetrics”类的所有链接,那么我建议您使用 XPath 来执行此操作。

查询时使用此表达式://a[contains(@class, 'coremetrics')]

【讨论】:

    【解决方案3】:

    结帐:http://php.net/manual/pl/class.domxpath.php 操作 DOM 树比使用正则表达式方式要容易得多..

    【讨论】:

      猜你喜欢
      • 2013-05-09
      • 1970-01-01
      • 2011-10-07
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 2023-01-05
      相关资源
      最近更新 更多