PHP Simple Html Dom gets simple div text but escapes all other tags

I am using PHP Simple Html Dom to get some html, now I have html dom, for example follow code, I need to get normal text inner div, but avoiding p tags and their content (only return 111111), who can help me? Thanks in advance!

<div>
    <p>00000000</p>
    111111
    <p>22222222</p>
</div>

      

+3


source to share


4 answers


It depends on what you mean by "tag avoidance".

If you just want to remove tags, then just run strip_tags()

on it what you want should work.



If you really want to just return "11111" (ie, separate the tags and their content ), this is not a viable solution. For this can work:

$myDiv = $html->find('div'); // wherever your the div you're ending up with is
$children = $myDiv->children; // get an array of children
foreach ($children AS $child) {
    $child->outertext = ''; // This removes the element, but MAY NOT remove it from the original $myDiv
}
echo $myDiv->innertext;

      

+8


source


If the text is always in the same position, try this:



$html->find('text', 2)->plaintext; // should return 111111

      

+5


source


Here is my solution

enter image description here

I only want to get a portion of the body text.

 $title_obj = $article->find(".ofr-descptxt",0); //Store the Original Tree  ie) h3 tag
 $title_obj->children(0)->outertext = ""; //Unset <br/>
 $title_obj->children(1)->outertext = "";  //Unset the last Span
 echo $title_obj; //It has only first element

      

Edited: If you have PHP errors Try wrap up with If else or try your lazy code

   ($title_obj->children(0))?$title_obj->children(0)->outertext="":"";
   ($title_obj->children(1))?$title_obj->children(1)->outertext = "":"";

      

Official documentation

+1


source


$wordlist = array("<p>", "</p>")

foreach($wordlist as $word)
     $string = str_replace($word, "", $string);

      

0


source







All Articles