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.. :'( Link to comment https://forums.phpfreaks.com/topic/205325-quick-question/ 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? Link to comment https://forums.phpfreaks.com/topic/205325-quick-question/#findComment-1074635 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 Link to comment https://forums.phpfreaks.com/topic/205325-quick-question/#findComment-1074636 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. Link to comment https://forums.phpfreaks.com/topic/205325-quick-question/#findComment-1074640 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 ) ) Link to comment https://forums.phpfreaks.com/topic/205325-quick-question/#findComment-1074650 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'; Link to comment https://forums.phpfreaks.com/topic/205325-quick-question/#findComment-1074653 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. Link to comment https://forums.phpfreaks.com/topic/205325-quick-question/#findComment-1074657 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.