simple html dom and text

2019-06-10 22:41发布

问题:

suppose that a simple html dom object includes the following

text1
<br />
<br />
<br />
text2
<br />

How can i get either of texts using simple html dom?

回答1:

To grab all plain text elements, you can use the following:

$string="text1
<br />
<br />
<br />
text2
<br />";

$html = str_get_html($string);
$texts=$html->find('text');
foreach($texts as $elem_index => $text){
    echo $elem_index."=>".$text."<br>";
}

Your output should look something like this:

0=>text1
1=>
2=>
3=> text2 

In the foreach loop, you can obviously do whatever filtering you need to. For instance, if you're trying to find some text that matches a specific string, you can simply do:

if (trim($text) == "text2")
    //do something

So, if you wanted to replace the found text, you could use:

if (trim($text) == "text2")
    $texts[$elem_index]->innertext = "something else";
$html->save();

and $html will contain the new html.



回答2:

Just use plaintext to get text value from html.

$opts = array(
'http' => array(
    'method' => "GET",
    'header' => "Accept-language: en\r\n" .
        "User-Agent:    Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; 
         rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6\r\n" .
        "Cookie: foo=bar\r\n"
     )
  );

$context = stream_context_create($opts);

$html1 = new simple_html_dom();
$html1->load_file($url, false, $context);
$appName1 = $html1->find('tagname.classname or id', 0)->plaintext;

For class use (tagname.classname) and for id use (tagname#id) to get text.

$context is for set header for browser support if any website get mobile version so u can add header to get browser support content.



回答3:

With jquery try $(document.body).contents();

This should you a array of node with the text1 and text2 as text node.