0

I am trying to make this function that extracts text from html for multiple steps but I cant figure out how to make a loop form it.

This is the task. I have this array

$arr = array("<div>" => "</div>", "<p>" => "</p>", "<h3>" => "</h3>");

The existing working function cut($a, $b, $c) that gets the content in between in this case $a = "<div>", $b="</div>" and $c = the html.

What i am trying to do is to make this:

  • Step 1 - cut from div to div
  • Step 2 - foreach results from step1, cut from p to p
  • Step 3 - foreach results from step2, cut from h3 to h3
  • Step n where n is array length.

Although I know that I can use foreach to get the results and apply step two, I cant generalize this.

EDIT

I have an existing function called cut that has been described above. I want to create a new function called cutlayer($arr, $html) where $arr is arr from above. I need the cutlayer function to use the cut function and do the following steps mentioned above but I cant figure out how to do that.

Thanks

Kartik
  • 9,463
  • 9
  • 48
  • 52

1 Answers1

3

Save yourself the trouble and use a toolkit designed for parsing HTML. PHP's DOMDocument is made for these tasks.

$dom = new DOMDocument();
$dom->loadHTML($yourHTML);
$divs = $dom->getElementsByTagName("div");

// Get the inner contents of all divs, for example
foreach ($divs as $div) {
  echo $div->nodeValue;
}

Unless this is homework and you were instructed to use your array matching method....

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • I guess but DomDocument wont help me cut between specific segments for example just between " and "
    " which my cut function can but I just cant figure out how to incorporate it to the loop
    – Kartik May 22 '11 at 21:28
  • @Kartik DOM can very much do that – Gordon May 22 '11 at 21:32
  • I suppose I didnt mention it but my original question still states that I am not sure about how to make that function because I cant figure out how to design the loops. I am not looking for extracting just html in general. The inputs can be like
    and
    then "class='foo' >" and "/b>" then "Welcome" & "here" the `cut` function can already take care of what to extract but I cant figure out how to start using the array
    – Kartik May 22 '11 at 21:59