Tags
Android
Asked 2 years ago
20 Jan 2022
Views 1421
kord

kord posted

How can I extract inner text of HTML tags <li> using Regex with preg_match_all in PHP?

I am not good at regular expression and want to get some info from some web pages so
I start with
file_get_contents("URL") to grab the page's HTML source. now the issue is I don't know how to get a specific part of the HTML

I get that preg_match_all() is useful to do this but I am clueless about how to do it

i want to get extract all in li tag , not just ui tag ,
below is sample HTML code :

 <li><span class="a-list-item">
     iPhone 7 </span>
</li>
 <li><span class="a-list-item"> iphone 8   </span></li> 
  <li><span class="a-list-item"> iPhone </li>
dilip

dilip
answered Jan 20 '22 00:00


$html=' <li><span class="a-list-item">
     iPhone 7 </span>
</li>
 <li><span class="a-list-item"> iphone 8   </span></li> 
  <li><span class="a-list-item"> iPhone </li>
';
  $regex = '/(<li>(.*)<\/li>)/isU';
 preg_match_all($regex, $html, $litext);
 print_r($litext);
Post Answer