briant Posted June 4, 2009 Share Posted June 4, 2009 These regular expressions are so hard to understand. Anyone know of a really good tutorial? My problem is this. $content = "<h2>hi</h2> 123 (cats)<br> 456 (dogs)<br> 789 (fishes)"; I want to be able to match 456 because of dogs. Usually I can do it if the pattern is before but since this is after, how can I do it? PS: This is for content I have no control over, so I can't change the content. Quote Link to comment Share on other sites More sharing options...
gevans Posted June 4, 2009 Share Posted June 4, 2009 There is a good starting tutorial here at phpfreaks http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax Also the sub forum http://www.phpfreaks.com/forums/index.php/board,43.0.html would've been a good starting place for your post! Quote Link to comment Share on other sites More sharing options...
gevans Posted June 4, 2009 Share Posted June 4, 2009 my regex isn't brilliant, but this should work; <?php preg_match('/([0-9]+) \(dogs\)/', $content, $matches); echo $matches[1]; ?> Quote Link to comment Share on other sites More sharing options...
briant Posted June 4, 2009 Author Share Posted June 4, 2009 Oh sorry, I should have looked through the other forums. Thank you for your help. I will try that out and let you know. I will try to read the tutorial as well. If it doesn't work out, I think I will just do a strstr function to get what I want. Thanks for your assistance. Quote Link to comment Share on other sites More sharing options...
briant Posted June 4, 2009 Author Share Posted June 4, 2009 It worked, thanks so much. My example was kind of stupid since the numbers 456 should've been anything including letters. But I'll figure it out. Thanks for your example gevans. You've been more than helpful. 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.