Get Text With PHP Simple HTML DOM Parser

I am using PHP Simple HTML DOM Parser to get text from a webpage. I need to manipulate a page:

<html>
<head>
<title>title</title>
<body>
<div id="content">
<h1>HELLO</h1>
Hello, world!
</div>
</body>
</html>

      

I need to get an element h1

and text that has no tags. for getting h1

i use this code:

$html = file_get_html("remote_page.html");
foreach($html->find('#content') as $text){
echo "H1: ".$text->find('h1', 0)->plaintext;
}

      

But a different text? I've also tried this in foreach, but I get the full text:

$text->plaintext;

      

but it also returned a tag h1

...

0


source to share


3 answers


It looks like it $text->find('text',2);

gets what you are looking for, but I'm not sure how well this will work when the number of text nodes is unknown. I will keep looking.



0


source


You can just strip the html tags with strip_tags



<?php
strip_tags($input, '<br>');
?>

      

0


source


Use stripe tags as @Peachy pointed out. However, passing the second argument to <br>

it means that the string will ignore tags <br>

, which is optional. In your case

<?php
    strip_tags($text);
?>

      

will work the way you would like, given that you are only selecting content in content

id.

0


source







All Articles