simple html dom and text

China☆狼群 提交于 2019-12-23 05:41:25

问题


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.



来源:https://stackoverflow.com/questions/10146631/simple-html-dom-and-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!