【问题标题】:Getting HTML elements out of string array in php [duplicate]从php中的字符串数组中获取HTML元素[重复]
【发布时间】:2014-04-08 03:47:14
【问题描述】:

我需要知道如何遍历这个数组并获取 html 内容,以便我可以使用它来制作另一个数组。我的数组是这样的:

$arr = array(
     "<span class='inside'>inside1</span> this is outside",
     "<span class='inside'>inside2</span> this is outside",
     "<span class='inside'>inside3</span> this is outside"
     );

我想要以下结果:

$result = array(
   "inside1",
   "inside2",
   "inside3"
   );

我尝试了以下但没有结果:

foreach ( $arr as $html){
   $dom = new DOMDocument();
   $dom->loadHTML($html);
   $xpath = new DOMXpath($dom);
   $result = $xpath->query('//span[@class="inside"]');
   echo $result
}

请帮忙。

【问题讨论】:

  • 你试过什么?
  • 我更新了我尝试过的内容

标签: php html arrays loops iteration


【解决方案1】:

你可以这样做

$arr = array(
    "<span class='inside'>inside1</span> this is outside",
    "<span class='inside'>inside2</span> this is outside",
    "<span class='inside'>inside3</span> this is outside"
);

$insideHTML = array();
foreach($arr as $string) {
    $pattern = "/<span ?.*>(.*)<\/span>/";
    preg_match($pattern, $string, $matches);
    $insideHTML[] = $matches[1];
}
var_dump($insideHTML);

这将为您提供以下数组

array(3) {
  [0]=>
  string(7) "inside1"
  [1]=>
  string(7) "inside2"
  [2]=>
  string(7) "inside3"
}

【讨论】:

    【解决方案2】:
    $arr = array(
         "<span class='inside'>inside1</span> this is outside",
         "<span class='inside'>inside2</span> this is outside",
         "<span class='inside'>inside3</span> this is outside"
         );
    
    function clean($var)
    {
    $var=strip_tags($var);
    $chunk=explode(' ',$var);
    return $chunk[0];
    
    }    
    
    $inside = array_map("clean", $arr);
    print_r($inside);
    

    【讨论】:

      【解决方案3】:

      对于您的确切示例,这将起作用:

      $arr = array(
           "<span class='inside'>inside1</span> this is outside",
           "<span class='inside'>inside2</span> this is outside",
           "<span class='inside'>inside3</span> this is outside"
           );
      
      $inside = array();
      
      foreach($arr as $k=>$v){
      
          $expl1 = explode("<span class='inside'>", $v);
      
          foreach($expl1 as $k2=>$v2){
      
              $v2 = trim($v2);
      
              if(strpos($v2, '</span>') !== false){
      
                  $expl2 = explode('</span>', $v2);
      
                  $inside[] = $expl2[0];
              }
      
          }
      
      }
      
      print_r($inside);
      

      产生:

      Array
      (
          [0] => inside1
          [1] => inside2
          [2] => inside3
      )
      

      【讨论】:

        【解决方案4】:

        如果这是您唯一需要做的事情(例如,它们的结构总是&lt;span&gt;Inside&lt;/span&gt;Oustide),您可以这样做:

        $result=array();
        foreach($arr as $html) {
            $result[]=substr(strstr(strstr($html,'>'),'<',true),1);
        }
        

        【讨论】:

          猜你喜欢
          • 2014-09-20
          • 2017-11-27
          • 1970-01-01
          • 2018-04-25
          • 2011-07-04
          • 1970-01-01
          • 1970-01-01
          • 2019-01-28
          • 2023-03-13
          相关资源
          最近更新 更多