N-Bomb(Nerd) Posted June 20, 2010 Share Posted June 20, 2010 I'm using preg_match to return a urls values, but some of the urls have the attribute title="text here" and some do not. Is there a way to make my regex say that the whole title="text here" part is optional? I'm currently using '/<a href="forum\.php\?f=(.*?)" title=".*?">(.*?)<\/a>/is' But that only returns the urls that have the title attribute.. :'( Quote Link to comment Share on other sites More sharing options...
N-Bomb(Nerd) Posted June 20, 2010 Author Share Posted June 20, 2010 Was the first post to confusing? Quote Link to comment Share on other sites More sharing options...
Alex Posted June 20, 2010 Share Posted June 20, 2010 Try this: /<a href="forum\.php\?f=(.*?)"[^>]*>(.*?)<\/a>/is Quote Link to comment Share on other sites More sharing options...
N-Bomb(Nerd) Posted June 20, 2010 Author Share Posted June 20, 2010 Try this: /<a href="forum\.php\?f=(.*?)"[^>]*>(.*?)<\/a>/is I just tried that and it's pulling items such as: <a href="forum.php?f=106" title="">Games</a> and <a href="forum.php?f=107" title="Forums for discussion of all games.">All Games</a> but it doesn't pull anything that looks like this: <a href="forum.php?f=108">Other Games</a> I'm not sure if it matters or not, but there's other markup right up against these anchor tags. Quote Link to comment Share on other sites More sharing options...
Alex Posted June 20, 2010 Share Posted June 20, 2010 Are you sure? It works for me: $text =<<<TEXT <a href="forum.php?f=108">Other Games</a> TEXT; preg_match_all('/<a href="forum\.php\?f=(.*?)"[^>]*>(.*?)<\/a>/is', $text, $matches); print_r($matches); Output: Array ( [0] => Array ( [0] => <a href="forum.php?f=108">Other Games</a> ) [1] => Array ( [0] => 108 ) [2] => Array ( [0] => Other Games ) ) Quote Link to comment Share on other sites More sharing options...
ZachMEdwards Posted June 20, 2010 Share Posted June 20, 2010 Try this: $pattern = '%<a href="forum\.php\?f=(\d+)".*?>(.[^<]+)</a>%si'; Quote Link to comment Share on other sites More sharing options...
N-Bomb(Nerd) Posted June 20, 2010 Author Share Posted June 20, 2010 Thanks. I got it working. Quote Link to comment 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.