Элемент PHP DOM не обрабатывает HTML-теги, такие как strong, anchor, div и т. Д. - PullRequest
1 голос
/ 08 ноября 2019

У нас точно такая же проблема, как и this

Я использовал этот код в своем проекте, и он работает нормально, но проблема в том, что я хочу загрузить все теги HTML, которыедоступны в этом теге div, например, strong, anchor, div и т. д., но я не знаю, как это сделать.

мы использовали следующий код:

<?php while (have_posts()) : the_post();

  ob_start();  // run the_content() through the Output Buffer
  the_content();
  $html = ob_get_clean(); // Store the formatted HTML
  $content = new DomDocument(); // Create a new DOMDocument Object to work with our HTML      
  $content->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'. $html );      
  $finder = new DomXPath( $content );  // Create a new DOMXPath object with our $content object that we will evaluate
  $classname = 'TEST'; // The class we are looking for

  $item_list = $finder->query("//*[contains(@class, '$classname')]"); // Evaluates our content and will return an object containing the number of items matching our query
?>
<div id = "result">`
<?php 
// echo the value of each item found
// You might want to format or wrap your $value according to your specification if necessary
  for( $i = 0; $i < $item_list->length; $i++ ){
    $value = $item_list->item( $i )->nodeValue;

    echo $value; //die();
    ?>

    <?php

    // if you want the text to be a link to the post, you could use this instead 
    // echo '<a href="' . get_the_permalink() . '">' . $value . '</a>';

} 
 ?>

, который выдает результат, как показано ниже:

Lorem ipsum dolor sit amet, consitteur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud упражнение ullamco labouris nisi ut aliquip ex ea кассовый следствие. Duis aute irure dolor в репереендерит в завитке Velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est labour.

но мы хотим получить ответ типа ниже

Lorem ipsum dolor sit amet

Lorem ipsum dolor sit amet , приверженец элиты, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud упражнение ullamco labouris nisi ut aliquip ex ea Коммунальный следствие . Duis aute irure dolor в репереендерит в завитке Velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est labour.

поэтому кто-нибудь может мне помочь с этим

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...