html - How to get text(file names) present between anchor tags in PHP? -


i've following strings me in file name present in between anchor tags:

  $test1 = test<div class="comment_attach_file">             <a class="comment_attach_file_link" href="http://52.1.47.143/feed/download/year_2015/month_04/file_3b701923a804ed6f28c61c4cdc0ebcb2.txt" >phase2 screen.txt</a><br>             <a class="comment_attach_file_link_dwl"  href="http://52.1.47.143/feed/download/year_2015/month_04/file_3b701923a804ed6f28c61c4cdc0ebcb2.txt" >download</a>             </div>;    $test2 =  holiday list.<div class="comment_attach_file">             <a class="comment_attach_file_link" href="http://52.1.47.143/feed/download/year_2015/month_04/file_2c96b997f03eefab317811e368731bb6.pdf" >holiday list-2013.pdf</a><br>             <a class="comment_attach_file_link_dwl"  href="http://52.1.47.143/feed/download/year_2015/month_04/file_2c96b997f03eefab317811e368731bb6.pdf" >download</a>             </div>;    $test3 = <div class="comment_attach_file">             <a class="comment_attach_file_link" href="http://52.1.47.143/feed/download/year_2015/month_04/file_8479c0b60867fdce35ae94a668dfbba9.docx" >sample2.docx</a><br>             </div>; 

from first string want text(i.e. file name) "phase2 screen.txt"

from second string want text(i.e. file name) "holiday list-2013.pdf"

from third string want text(i.e. file name) "sample2.docx"

how should in php using $dom = new domdocument;?

please me.

thanks.

you can use domxpath target link contains text want, use class point it:

$dom = new domdocument; for($i = 1; $i <= 3; $i++) {     @$dom->loadhtml(${"test{$i}"});     $xpath = new domxpath($dom);     $file_name = $xpath->evaluate('string(//a[@class="comment_attach_file_link"])');     echo $file_name , '<br/>'; } 

or if don't want use xpath, can anchor elements , check class, if one, ->nodevalue:

$dom = new domdocument; for($i = 1; $i <= 3; $i++) {     @$dom->loadhtml(${"test{$i}"});     foreach($dom->getelementsbytagname('a') $anchor) {         if($anchor->getattribute('class') === 'comment_attach_file_link') {             echo $anchor->nodevalue, '<br/>';             break;         }     } } 

sample output


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -