newb Posted April 5, 2010 Share Posted April 5, 2010 can someone tell me whats wrong with this code: if (preg_match('|movie <a href="/encyclopedia/(.*?)">(.*) \(('.$type.')\)</a><br />$|Ui',$html,$result)) { its not working =// the variable isnt being detected, Quote Link to comment https://forums.phpfreaks.com/topic/197585-variable-in-regex/ Share on other sites More sharing options...
Psycho Posted April 5, 2010 Share Posted April 5, 2010 I don't think the problem is your variable - I think it is just the regex expression in general. But, I would suggest: 1) Testing the regex with a hard coded value first to ensure it is working properly, THEN introduce the variable 2) creating the expression as a string variable outside of the actual preg_match. then you can echo the expression to the page for debugging purposes. Without seeing what you are using for the $type there's no way to really know what the problem is. Although I do see some potential problems. 1. You use this right after the opening A tag "(.*)". That is a 'greedy' match, so it will likely match more characters than you want since the parser doesn't know when to end. you should probably use the question mark as you do in the previous usage 2. You then follow that by a space. Don't know if that is intentional or not. 2. Quote Link to comment https://forums.phpfreaks.com/topic/197585-variable-in-regex/#findComment-1036991 Share on other sites More sharing options...
newb Posted April 5, 2010 Author Share Posted April 5, 2010 when i test it without a variable the expression works fine... $type is set to = 'movie' when i put 'movie' instead of type, it works, but thats not what im trying to fix.. Quote Link to comment https://forums.phpfreaks.com/topic/197585-variable-in-regex/#findComment-1037006 Share on other sites More sharing options...
newb Posted April 5, 2010 Author Share Posted April 5, 2010 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/197585-variable-in-regex/#findComment-1037407 Share on other sites More sharing options...
newb Posted April 5, 2010 Author Share Posted April 5, 2010 fix Quote Link to comment https://forums.phpfreaks.com/topic/197585-variable-in-regex/#findComment-1037423 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.