Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中从字符串数组中获取HTML元素_Php_Html_Arrays_Loops_Iteration - Fatal编程技术网

在php中从字符串数组中获取HTML元素

在php中从字符串数组中获取HTML元素,php,html,arrays,loops,iteration,Php,Html,Arrays,Loops,Iteration,我需要知道如何迭代这个数组并获取html内容,以便使用它生成另一个数组。我拥有的阵列是: $arr = array( "<span class='inside'>inside1</span> this is outside", "<span class='inside'>inside2</span> this is outside", "<span class='inside'>inside3</sp

我需要知道如何迭代这个数组并获取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"
     );
我尝试了以下方法,但没有结果:

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

你可以这样做

$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);
$arr=array(
“内部1这是外部”,
“内部2这是外部”,
“内部3这是外部”
);
函数清理($var)
{
$var=带标签($var);
$chunk=explode(“”,$var);
返回$chunk[0];
}    
$inside=array_map(“clean”、$arr);
打印(内部);

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

$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);

如果这是您唯一需要做的事情(如中所述,它们的结构总是
insideOutide
),您可以这样做:

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

$result[]=substr(strstrstr(strstrstr($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"
     );

function clean($var)
{
$var=strip_tags($var);
$chunk=explode(' ',$var);
return $chunk[0];

}    

$inside = array_map("clean", $arr);
print_r($inside);
$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
)
$result=array();
foreach($arr as $html) {
    $result[]=substr(strstr(strstr($html,'>'),'<',true),1);
}