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
https://forums.phpfreaks.com/topic/258404-preg_match_all-help/
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
https://forums.phpfreaks.com/topic/258404-preg_match_all-help/#findComment-1324578
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.