【问题标题】:RegExp PHP get text between multiple span tagsRegExp PHP 获取多个跨度标签之间的文本
【发布时间】:2011-06-01 01:41:30
【问题描述】:

我的英语说得不太好。所以,如果我犯了一些错误,请见谅。

在网站上,我有一个 div 框,其中包含一些有关游戏的信息:

<span class="noteline">Developer:</span> 
<span class="subline">Gameloft</span> 
<span class="noteline">Genre:</span> 
<span class="subline">Racing/Arcade</span> 
<span class="noteline">Release year:</span> 
<span class="subline">2010</span> 

我需要获取&lt;span class="noteline"&gt; 和结束标签&lt;/span&gt; 之间的信息

preg_match("/\<span\sclass=\"subline\"\>(.*)<\/span\>/imsU", $source, $matches);

上面的解决方案工作正常,但它只获得带有文本“gameloft”的“subline”;

但我还需要包含 Racing/Arcade 和 2010 文本的子行;

也许是这样的(那行不通);

for developer = preg_match("/*(\<span\sclass=\"subline\"\>){1}*(.*)*(<\/span\>){1}*/imsU", $source, $matches);
for genre = preg_match("/*(\<span\sclass=\"subline\"\>){2}*(.*)*(<\/span\>){2}*/imsU", $source, $matches);

类似的东西..

无论如何。感谢您的帮助。

【问题讨论】:

    标签: php regex tags match


    【解决方案1】:

    试试这个:

    preg_match_all("/<span class=\"subline\".*span>/", $html, $matches);
    
    preg_match_all("/<span class=\"noteline\".*span>/", $html, $matches);
    

    我用这种方式尝试了上面的代码:

    <?php 
    
    $html = '<span class="noteline">Developer:</span> 
    <span class="subline">Gameloft</span> 
    <span class="noteline">Genre:</span> 
    <span class="subline">Racing/Arcade</span> 
    <span class="noteline">Release year:</span> 
    <span class="subline">2010</span>';
    
    preg_match_all("/<span class=\"subline\".*span>/", $html, $matches1);
    
    preg_match_all("/<span class=\"noteline\".*span>/", $html, $matches2);
    
    print_r($matches1);
    echo "<br>";
    print_r($matches2);
    
    ?>
    

    我得到的输出是这样的:

    Array ( [0] => Array ( [0] => Gameloft [1] => Racing/Arcade [2] => 2010 ) )
    Array ( [0] => Array ( [0] => Developer: [1] => Genre: [2] => Release year: ) ) 
    

    【讨论】:

    • 非常感谢。这似乎是一个很好的解决方案。
    【解决方案2】:

    正则表达式的替代方法是使用 phpQuery 或 QueryPath,这将其简化为:

    foreach ( qp($source)->find("span.subline") as $span ) {
        print $span->text();
    }
    

    【讨论】:

      【解决方案3】:

      正则表达式不适合解析 HTML。它们很难正确处理,而且总是在极端情况下中断。

      我不知道是否有更简单的方法,但这应该适用于您描述的标记:

      <?php
      
      $fragment = '<span class="noteline">Developer:</span>
      <span class="subline">Gameloft</span>
      <span class="noteline">Genre:</span>
      <span class="subline">Racing/Arcade</span>
      <span class="noteline">Release year:</span>
      <span class="subline">2010</span>';
      
      libxml_use_internal_errors(TRUE);
      $dom = new DOMDocument();
      $dom->loadHTML($fragment);
      $xml = simplexml_import_dom($dom);
      libxml_use_internal_errors(FALSE);
      
      foreach($xml->xpath("//span[@class='subline']") as $item){
          echo (string)$item . PHP_EOL;
      }
      

      这假定class="subline" 因此它会因多个类而失败。 (Xpath 新手,欢迎改进。)

      【讨论】:

      • 正是我需要的,不知道那个 DOMDocument 类,谢谢!
      猜你喜欢
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      相关资源
      最近更新 更多