imperium2335 Posted October 21, 2009 Share Posted October 21, 2009 Hi, I can't get the expression to work for my preg_match, what I'd like is for it to match the string, but only if it is accompanied by a letter, not a number. So if a string was found with a number after it, it would pass, but fail if there where letters. I have this but it doesn't work: if(!preg_match("/table-/+[abcdefghijklmnopqrstuvwxyz]", $string, $dump)) { //do stuff } Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/178410-solved-preg-match-with-a-string-and-numbers/ Share on other sites More sharing options...
MadTechie Posted October 21, 2009 Share Posted October 21, 2009 Can you give an example of what your expect to pass and fail ie A. table-12 = pass B. table-123 = pass C. table-1a3 = fail or pass ? D. table-abc = fail E. mytable-1 = fail or pass ? With C/E would it pass as its "table-1" ? What does it match up to ? ie name="table-12" would match the text from " to " Quote Link to comment https://forums.phpfreaks.com/topic/178410-solved-preg-match-with-a-string-and-numbers/#findComment-940825 Share on other sites More sharing options...
imperium2335 Posted October 21, 2009 Author Share Posted October 21, 2009 hi, table-24234 = pass table-1 = pass table-name = fail table-cheese134 = fail table- must just be "table-" its actually checking a html tag, to expand alittle: id="table-23421" as an example, but i dont want it to pass id="table-name" etc Thanks for your reply. Quote Link to comment https://forums.phpfreaks.com/topic/178410-solved-preg-match-with-a-string-and-numbers/#findComment-940829 Share on other sites More sharing options...
MadTechie Posted October 21, 2009 Share Posted October 21, 2009 try this Check only if (preg_match('/id="table-\d+"/', $string)) { echo "valid" } get the number if (preg_match('/id="table-(\d+)"/', $string,$dump)) { echo $dump[1]; } Quote Link to comment https://forums.phpfreaks.com/topic/178410-solved-preg-match-with-a-string-and-numbers/#findComment-940834 Share on other sites More sharing options...
imperium2335 Posted October 21, 2009 Author Share Posted October 21, 2009 Thanks, i will give it a go. Do you know any good tutorials on these expression? I've read the manual and still cant get my head around them Quote Link to comment https://forums.phpfreaks.com/topic/178410-solved-preg-match-with-a-string-and-numbers/#findComment-940835 Share on other sites More sharing options...
MadTechie Posted October 21, 2009 Share Posted October 21, 2009 Great resource is right here its mainly simple ones and practice ie if (preg_match('/id="table-\d+"/', $string)) { echo "valid" } finds id="table- then a \d (digit) + (means 1 or more) and then finds a " Now if it finds the digits but then NOT a " it won't match so 12b" would fail on my second example i put () around the \d+ this means capture (store in $dump) now $dump[0] = the full matched string $dump[1] = the first captured item okay that's a break down of what you have.. hope that helps and doesn't scare you away it takes time when I first saw them i was but they seam simpler now Quote Link to comment https://forums.phpfreaks.com/topic/178410-solved-preg-match-with-a-string-and-numbers/#findComment-940845 Share on other sites More sharing options...
imperium2335 Posted October 21, 2009 Author Share Posted October 21, 2009 Thanks! It worked to Quote Link to comment https://forums.phpfreaks.com/topic/178410-solved-preg-match-with-a-string-and-numbers/#findComment-940853 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.