R Shahpari
R Shahpari

Reputation: 71

need regular expression for li

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>

Upvotes: 0

Views: 65

Answers (2)

Pedro Amaral Couto
Pedro Amaral Couto

Reputation: 2115

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
)

Upvotes: 0

Ram Sharma
Ram Sharma

Reputation: 8819

you can try this

$myPattern = "/<li class=\"release\">(.*?)<\/li>/s";
$myText = '<li class="release">*</li>';
preg_match($myPattern,$myText,$match);
echo $match[1];

Upvotes: 2

Related Questions