2

I'm getting tired of looking around for a close enough example, time for some quick help! Here is my code:

preg_match_all( '#<li.*?>.*?</li>#s', $card_html, $activity );

I want to modify it so that the <li.*?> excludes the word Unplayed. (The word comes after the .*? and before the >.

Edit:

Want to catch: http://gamercard.xbox.com/en-US/Stallion83.card

            <li >

                <a href="http://live.xbox.com/en-us/GameCenter/Achievements?title=1464993792&amp;compareTo=Stallion83">
                   <img src="http://tiles.xbox.com/tiles/vD/fP/1Gdsb2JhbA9ECgUPGgIfVl9TL2ljb24vMC84MDAwIAAAAAAAAPvgN6M=.jpg" alt="F.E.A.R. 3" title="F.E.A.R. 3" />
                   <span class="Title">F.E.A.R. 3</span>
                   <span class="LastPlayed">6/24/2011</span>
                   <span class="EarnedGamerscore">415</span>
                   <span class="AvailableGamerscore">1000</span>
                   <span class="EarnedAchievements">23</span>
                   <span class="AvailableAchievements">50</span>
                   <span class="PercentageComplete">46%</span>
                </a>
            </li>

            <li class="Complete" >

                <a href="http://live.xbox.com/en-US/GameCenter/Achievements?title=1096157212&amp;compareTo=Im%20RedJ">
                   <img src="http://tiles.xbox.com/tiles/HI/L4/1Gdsb2JhbA9ECgQJGgYfVl4gL2ljb24vMC84MDAwIAAAAAAAAPvXggM=.jpg" alt="Call of Duty: WaW" title="Call of Duty: WaW" />
                   <span class="Title">Call of Duty: WaW</span>
                   <span class="LastPlayed">6/21/2011</span>
                   <span class="EarnedGamerscore">1500</span>
                   <span class="AvailableGamerscore">1500</span>
                   <span class="EarnedAchievements">66</span>
                   <span class="AvailableAchievements">66</span>
                   <span class="PercentageComplete">100%</span>
                </a>
            </li>

Dont want: http://gamercard.xbox.com/en-US/test.card

            <li class="Unplayed"></li>

            <li class="Unplayed"></li>

Thanks.

2
  • You are already doing something wrong if you use regex to parse HTML. What about a proper HTML parser? Commented Jun 25, 2011 at 13:03
  • Because I don't need that much overhead for the project I'm doing. Commented Jun 25, 2011 at 13:04

2 Answers 2

5

Try this. The lookahead makes sure that the rest of the <li> tag does not contain Unplayed.

preg_match_all( '#<li(?=(.(?!Unplayed))*?>).*?>(.(?!Unplayed))*?</li>#s', $card_html, $activity ); 

Edit: I couldn't tell from your example, but it sounds like unplayed can occur both outside and inside the begin tag for <li>.

Sign up to request clarification or add additional context in comments.

8 Comments

Close. There are 5 results by default. Your attempt removes all but 1 of the contents containing Unplayed.
Oh. Great. So if you post your example html, I'm sure we can get this figured out.
I've added the link from where I'm pulling the info from.
@Dark, take a look at the new one.
Its still returning one empty recent game.
|
1

Regex is not the right tool for this task and you can see many explanations on SO for this. Use DOMDocument like this:

function innerHTML($node){
  $doc = new DOMDocument();
  foreach ($node->childNodes as $child)
    $doc->appendChild($doc->importNode($child, true));   
  return $doc->saveHTML();
}
$dom = new DOMDocument();
$dom->loadHTML($content);
// To hold all your liv...
$lis = array();
// Get all li nodes
$liNodes = $dom->getElementsByTagName("li");
foreach($liNodes as $liNode) {
  // Check the class attr of each li
  $cl = $liNode->getAttribute("class");
  if ($cl != "Unplayed")
       $lis[] = innerHTML($liNode);
}
print_r($lis);

OUTPUT for above input html:

Array
(
    [0] => 
                <a href="http://live.xbox.com/en-us/GameCenter/Achievements?title=1464993792&amp;compareTo=Stallion83">
                   <img src="http://tiles.xbox.com/tiles/vD/fP/1Gdsb2JhbA9ECgUPGgIfVl9TL2ljb24vMC84MDAwIAAAAAAAAPvgN6M=.jpg" alt="F.E.A.R. 3" title="F.E.A.R. 3"><span class="Title">F.E.A.R. 3</span>
                   <span class="LastPlayed">6/24/2011</span>
                   <span class="EarnedGamerscore">415</span>
                   <span class="AvailableGamerscore">1000</span>
                   <span class="EarnedAchievements">23</span>
                   <span class="AvailableAchievements">50</span>
                   <span class="PercentageComplete">46%</span>
                </a>            

    [1] => 
                <a href="http://live.xbox.com/en-US/GameCenter/Achievements?title=1096157212&amp;compareTo=Im%20RedJ">
                   <img src="http://tiles.xbox.com/tiles/HI/L4/1Gdsb2JhbA9ECgQJGgYfVl4gL2ljb24vMC84MDAwIAAAAAAAAPvXggM=.jpg" alt="Call of Duty: WaW" title="Call of Duty: WaW"><span class="Title">Call of Duty: WaW</span>
                   <span class="LastPlayed">6/21/2011</span>
                   <span class="EarnedGamerscore">1500</span>
                   <span class="AvailableGamerscore">1500</span>
                   <span class="EarnedAchievements">66</span>
                   <span class="AvailableAchievements">66</span>
                   <span class="PercentageComplete">100%</span>
                </a> 
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.