Jump to content

preg_match_all help!


Codemunkie

Recommended Posts

Hello, i'm trying to retrieve some information from another website using preg match all.

 

Here is my current code i have, it works for my first match but no others.

<?php
$content = file_get_contents('http://leagueoflegends.wikia.com/wiki/Free_champion_rotation');

preg_match_all('#class="mw-redirect">(.*)</a></span></span></span>#', $content, $match, PREG_SET_ORDER);
echo $match[0][1];
?>

 

The html i am wanting to parse is here.

class="mw-redirect">Ahri</a></span></span></span>
class="mw-redirect">Galio</a></span></span></span>
class="mw-redirect">Graves</a></span></span></span>
class="mw-redirect">Katarina</a></span></span></span>
class="mw-redirect">Kog'Maw</a></span></span></span>
class="mw-redirect">LeBlanc</a></span></span></span>
class="mw-redirect">Shen</a></span></span></span>
class="mw-redirect">Skarner</a></span></span></span>
class="mw-redirect">Soraka</a></span></span></span>
class="mw-redirect">Warwick</a></span></span></span>

 

I'm only wanting to display the names after the class, which it does for 'Ahri' but nothing else.

 

Can anyone help me with this?

Link to comment
Share on other sites

Hi Codemunkie!

 

Here is some working code for you:

 

Code:

<?php
$string='
class="mw-redirect">Ahri</a></span></span></span>
class="mw-redirect">Galio</a></span></span></span>
class="mw-redirect">Graves</a></span></span></span>
class="mw-redirect">Katarina</a></span></span></span>
class="mw-redirect">Kog\'Maw</a></span></span></span>
class="mw-redirect">LeBlanc</a></span></span></span>
class="mw-redirect">Shen</a></span></span></span>
class="mw-redirect">Skarner</a></span></span></span>
class="mw-redirect">Soraka</a></span></span></span>
class="mw-redirect">Warwick</a></span></span></span>
';
$regex='#class="mw-redirect">(.*?)</a>#';
preg_match_all($regex, $string, $matches, PREG_PATTERN_ORDER);
$sz=count($matches[1]);
for ($i=0;$i<$sz;$i++) 
echo $matches[1][$i]."<br />";
?>

 

Output:

Ahri

Galio

Graves

Katarina

Kog'Maw

LeBlanc

Shen

Skarner

Soraka

Warwick

 

The tweaks:

- you needed a loop to iterate through the captures

- I made your dot-star (.*) lazy (the question mark). (Here is my page on greedy and lazy quantifiers if you'd like to read up on them.) Otherwise, the "greedy star" would cause the regex engine to roll all the way down to the end of the string, then to backtrack to the last </a> etc, giving you a much longer capture than you want.

 

Let me know if you have any questions. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.