Search code examples
phpsimple-html-dom

Removing with ->outertext is not removing


I have the following code:

<div id="coursename">
  <h1>My Golf Club<br>
  <span class="courseregion">My Region</span></h1>
</div>

What I want to do is get the course name and it's region. Separately. Now, because the region is inside the #coursename element, I firstly want to get .courseregion, then remove it so I don't end up with My Golf ClubMy Region

This is what I'm trying but it still returns both together:

$course_region = $html->find('.courseregion', 0);

$region_to_use = $course_region; // stored

$course_region->outertext = ""; // get rid of course region

$course_name = $html->find('#coursename', 0);

echo $course_name->plaintext; // returns -> My Golf ClubMy Region

Where am I going wrong? Any ideas?

UPDATE I cannot modify the html, it is what it is


Solution

  • Just work with the strings, don't try to modify the HTML. In this case:

    // the element you're showing has an id, so there is only ever one
    $cn = $html->find('#coursename');
    $h1 = $cn->find('h1');
    
    // get both the "full" text, and the "text we don't want":
    $a = $h1.innertext;
    $b = $h1->find('span').innertext;
    
    // now we just remove $b from $a.
    // We don't need to edit the HTML to achieve that:
    $actual_text = str_replace($b, '', $a);