Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I need to know how to iterate through this array and get the html content so that I can use it to make another array. The array I have is this:

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

and I want the following result:

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

I have tried the following but no results:

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

Please Help.

share|improve this question

marked as duplicate by scrowler, Marc B, Appleman1234, Marco A., EdChum Mar 18 at 8:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
And what have you tried? –  Daniel Figueroa Mar 5 at 20:40
    
I updated with what I tried –  Diego Camacho Mar 5 at 20:44

4 Answers 4

up vote 1 down vote accepted
$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);
share|improve this answer

You could do this

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

This will then give you the following array

array(3) {
  [0]=>
  string(7) "inside1"
  [1]=>
  string(7) "inside2"
  [2]=>
  string(7) "inside3"
}
share|improve this answer

For your exact example this would work:

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

Which produces:

Array
(
    [0] => inside1
    [1] => inside2
    [2] => inside3
)
share|improve this answer

If this is the only thing you need to do (as in, they are always going to be structured as <span>Inside</span>Oustide) You could do this:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.