0

How can I get the Strings between my li tags in php? I have tried many php code but it does not work.

<li class="release">
    <strong>Release info:</strong>
    <div>
        How.to.Train.Your.Dragon.2.2014.All.BluRay.Persian
    </div>
    <div>
        How.to.Train.Your.Dragon.2.2014.1080p.BRRip.x264.DTS-JYK
    </div>
    <div>
        How.to.Train.Your.Dragon.2.2014.720p.BluRay.x264-SPARKS
    </div>
</li>
Mikel Urkia
  • 2,087
  • 1
  • 23
  • 40
R Shahpari
  • 71
  • 1
  • 1
  • 4

2 Answers2

2

you can try this

$myPattern = "/<li class=\"release\">(.*?)<\/li>/s";
$myText = '<li class="release">*</li>';
preg_match($myPattern,$myText,$match);
echo $match[1];
Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
0

You don't need a regular expression. It seems to be a common mistake to use regular expressions to parse HTML code (I took the URL from T.J. Crowder comment).

Use a tool to parse HTML, for instance: DOM library.

This is a solution to get all strings (I'm assuming those are the values of the text nodes):

$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$nodes = $xpath->query('//li//text()');
$strings = array();
foreach($nodes as $node) {
    $string = trim($node->nodeValue);
    if( $string !== '' ) {
        $strings[] = trim($node->nodeValue);
    }
}

print_r($strings); outputs:

Array
(
    [0] => Release info:
    [1] => How.to.Train.Your.Dragon.2.2014.All.BluRay.Persian
    [2] => How.to.Train.Your.Dragon.2.2014.1080p.BRRip.x264.DTS-JYK
    [3] => How.to.Train.Your.Dragon.2.2014.720p.BluRay.x264-SPARKS
)
Community
  • 1
  • 1
Pedro Amaral Couto
  • 2,056
  • 1
  • 13
  • 15