Codemunkie Posted March 6, 2012 Share Posted March 6, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/258404-preg_match_all-help/ Share on other sites More sharing options...
ragax Posted March 6, 2012 Share Posted March 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258404-preg_match_all-help/#findComment-1324578 Share on other sites More sharing options...
Codemunkie Posted March 6, 2012 Author Share Posted March 6, 2012 Works like a charm! Thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/258404-preg_match_all-help/#findComment-1324596 Share on other sites More sharing options...
ragax Posted March 6, 2012 Share Posted March 6, 2012 Glad to hear it, Codemunkie, you're very welcome. Quote Link to comment https://forums.phpfreaks.com/topic/258404-preg_match_all-help/#findComment-1324605 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.