使用 ->outertext 删除并没有删除

Removing with ->outertext is not removing

我有以下代码:

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

我想做的是获取课程名称及其所在地区。分别地。现在,因为该区域在 #coursename 元素内,我首先想要获取 .courseregion,然后将其删除,这样我就不会得到 My Golf ClubMy Region

这就是我正在尝试的方法,但它仍然 returns 两者都在一起:

$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

我哪里错了?有什么想法吗?

UPDATE 我无法修改 html,就是这样

只处理字符串,不要尝试修改 HTML。在这种情况下:

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

您可以使用 str_replace 并从 $course_name-> 纯文本中删除 $region_to_use;

//this will remove $region_to_use from $course_name->plaintext
echo(substr_replace ($region_to_use, '', $course_name->plaintext);

这是因为 simple 没有更新明文(这是一个错误):

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

$doc = str_get_html($html);
$doc->find('.courseregion', 0)->outertext = "";

echo $doc->find('#coursename', 0)->plaintext . "\n";
//    My Golf Club    My Region   

$doc = str_get_html((string)$doc); // reload $doc (or switch to http://sourceforge.net/projects/advancedhtmldom/?source=directory)

echo $doc->find('#coursename', 0)->plaintext . "\n";
//    My Golf Club