How to get text before running a specific html tag using html dom parser PHP

I can't figure out how to get text between html tags. in my script it is required that the text should not be wrapped between tags except the paragraph tag <p>.

<div class="entry clearfix">
<p>111</p>
<p><img class="alignnone size-medium wp-image-38376" src="1.jpg" alt="Talvar" /></p>
<p><strong>111: </strong>111<br/>
    <strong>111:</strong> 111<br/>
    <strong>111:</strong> 111 111<br/>
    <strong>111: </strong>111<br/>
    <strong>111: </strong>1111
</p>
<p><strong>111</strong></p>
<p>
    <strong>01 &#8211;</strong> data1 <strong><a href="#">Download</a><br/>
    </strong><em>222</em><br/>
    <strong>02 &#8211;</strong> data2 <strong><a href="#">Download</a><br/>
    </strong><em>222</em><br/>
    <strong>03 &#8211;</strong> data3 <strong><a href="#">Download</a><br/>
    </strong><em>222</em><br/>
    <strong>04 &#8211;</strong> data4 <strong><a href="#">Download</a><br/>
    </strong><em>222</em>
</p>
<p><strong>222</strong></p>
<p><strong><a href="" target="_blank">3333</a></strong></p>
<p><strong>eb</strong></p></div>

I need data1, data2, data3, data4. for this I find <p>which is number 5, as in array number 4.

    foreach($html->find('div[class="entry"]') as $row){
        $a = $row->find('p',4);
        echo $dt = $a->find('text',1)->plaintext; // returns me only data1
    }

data1, data2, data3, data4 are not between any tags, except <p>if I get them through striptags(), it returns all texts along with 111, Download, 222, etc., please tell me how I can get a series of data.

+4
source share
1 answer

, :

foreach($html->find('div[class="entry"]') as $row){
$a = $row->find('p',4);

$str=$a->find('strong');
$em=$a->find('em');

foreach($str as $tag) {

$a=str_replace($tag,'',$a);
$a=str_replace($em,'',$a);


        }

}

echo strip_tags($a,'<br>'); // if you want to keep br tags

, - strong em ( , ), p, str_replace . HTML , , .

+1

Source: https://habr.com/ru/post/1610202/


All Articles