N-Bomb(Nerd) Posted June 17, 2009 Share Posted June 17, 2009 What exactly would I be doing wrong with this expression? preg_match_all('/<a href="lyrics\.php\?id=(.*?)">(.*?)<img src="(.*?)"><\/a>/is', $String, $Title); What I'm mainly going after is the second search, however the other two are always random. Example: <a href="lyrics.php?id=5352">Limp Bizkit - Boiler<img src="hot.gif"></a> Link to comment https://forums.phpfreaks.com/topic/162569-solved-preg_match_all-problem/ Share on other sites More sharing options...
Maq Posted June 17, 2009 Share Posted June 17, 2009 Try: $s = "Limp Bizkit - Boiler"; $pattern = '~^(.*)$~i'; preg_match_all($pattern, $s, $matches); print_r($matches); ?> - You were pretty close (not that mine's perfect) but I made some changes. - If 'id' is always going to be an integer you should use ([0-9]*). - You should use \s for whitespaces. In the $matches array you have to go through and figure out what each key goes to each match. The array dump would look like this: Array ( [0] => Array ( [0] => Limp Bizkit - Boiler ) [1] => Array ( [0] => 5352 ) [2] => Array ( [0] => Limp Bizkit - Boiler ) [3] => Array ( [0] => hot.gif ) ) Hope this helps, good luck. Link to comment https://forums.phpfreaks.com/topic/162569-solved-preg_match_all-problem/#findComment-858024 Share on other sites More sharing options...
thebadbad Posted June 17, 2009 Share Posted June 17, 2009 What exactly would I be doing wrong with this expression? preg_match_all('/<a href="lyrics\.php\?id=(.*?)">(.*?)<img src="(.*?)"><\/a>/is', $String, $Title); What I'm mainly going after is the second search, however the other two are always random. Example: <a href="lyrics.php?id=5352">Limp Bizkit - Boiler<img src="hot.gif"></a> Your code works fine when I'm testing it. What went wrong for you? If you're only looking for one match, you should use preg_match() instead of preg_match_all() for better efficiency. @Maq - If you're sure that you're matching a space, it's perfectly fine to simply use a literal space in the pattern. But yeah, \s is more versatile (matching most/all kinds of whitespace) and I often prefer it over literal spaces in these type of cases. - You added start and end of string (^ and $) to your pattern. That would keep the pattern from matching the data if it is part of a larger source code, which I would guess it is. - You also removed the s modifier (or didn't add it). It's always a good idea to use the s modifier when using the dot, if a newline should appear within the desired match @OP You're a Bizkit fan? Watched the live stream of their concert @ Download Festival last weekend - freakin' awesome gig! Link to comment https://forums.phpfreaks.com/topic/162569-solved-preg_match_all-problem/#findComment-858093 Share on other sites More sharing options...
Maq Posted June 17, 2009 Share Posted June 17, 2009 - If you're sure that you're matching a space, it's perfectly fine to simply use a literal space in the pattern. But yeah, \s is more versatile (matching most/all kinds of whitespace) and I often prefer it over literal spaces in these type of cases. I agree. - You added start and end of string (^ and $) to your pattern. That would keep the pattern from matching the data if it is part of a larger source code, which I would guess it is. Yes I know. Not sure why I did that. - You also removed the s modifier (or didn't add it). It's always a good idea to use the s modifier when using the dot, if a newline should appear within the desired match Yeah, I forgot to add it back in. Thanks for the info. Link to comment https://forums.phpfreaks.com/topic/162569-solved-preg_match_all-problem/#findComment-858097 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.