+12 Spell Power and +1...">

How to make this regex match </span>

This is the type of HTML string in which I will perform matches:

<span class="q1">+12 Spell Power and +10 Hit Rating</span>

I want to get +12 Spell Power and +10 Hit Ratingfrom the above HTML. This is the code I wrote:

preg_match('/<span class="q1">(.*)<\/span>/', $gem, $match);

But because of <\/span>it it eludes /in </span>, so it does not stop the match, so I get a lot more data than I want.

How can I escape /in </span>while retaining part of it?

Thank.

+3
source share
3 answers

, , , , - , * , . *?, :

preg_match('/<span class="q1">(.*?)<\/span>/', $gem, $match);
+2
  • HTML
  • DOM, loadHTML getElementsByTagName('span')

-

    $doc = new DOMDocument();
    $doc->loadHTML($htmlString);
    $spans = $doc->getElementsByTagName('span');
    if ( $spans->length > 0 ) {
     // loop on $spans
    }
+2

Do not use regex to parse HTML. Use an HTML parser. See Reliable, mature HTML parser for PHP .

+2
source

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


All Articles