skwap Posted September 1, 2011 Share Posted September 1, 2011 Friends. i have a problem while using preg_match() function. My aim is to match the value of n from this url index.php?n=100&img=1 i used this code $url = "index.php?n=100&img=1"; preg_match('/\index.php?n=(.*?)&img/si',$mid,$url); print_r($mid); after executing above code the code did not print the matched array values. Its printing just like this Array() i want to print the matched value of n from my url. Anyone can help me to match the value ? & please correct my code. Thanks In Advance Quote Link to comment https://forums.phpfreaks.com/topic/246158-preg_match-help/ Share on other sites More sharing options...
cags Posted September 1, 2011 Share Posted September 1, 2011 To start with you have url and mid the wrong way round (which 2 seconds with the manual would have told you). Quote Link to comment https://forums.phpfreaks.com/topic/246158-preg_match-help/#findComment-1264177 Share on other sites More sharing options...
silkfire Posted September 1, 2011 Share Posted September 1, 2011 skwap, may I ask - is the index.php the site you're currently on? IF so you can retrieve the query strings (&n=) with the $_SERVER variable. Quote Link to comment https://forums.phpfreaks.com/topic/246158-preg_match-help/#findComment-1264189 Share on other sites More sharing options...
AyKay47 Posted September 1, 2011 Share Posted September 1, 2011 seems like you have your arguments switched.. the second argument should be the subject that your pattern applies to, and the third argument will be the array of matches... preg_match('/\index.php?n=(.*?)&img/si',$url,$mid); //switch $mid and $url Quote Link to comment https://forums.phpfreaks.com/topic/246158-preg_match-help/#findComment-1264228 Share on other sites More sharing options...
salathe Posted September 1, 2011 Share Posted September 1, 2011 Just in case it wasn't clear from the replies above, skwap, you have your arguments switched. $url and $mid should be the other way around. P.S. See, I can echo too! Quote Link to comment https://forums.phpfreaks.com/topic/246158-preg_match-help/#findComment-1264246 Share on other sites More sharing options...
AyKay47 Posted September 1, 2011 Share Posted September 1, 2011 Just in case it wasn't clear from the replies above, skwap, you have your arguments switched. $url and $mid should be the other way around. P.S. See, I can echo too! congrats Quote Link to comment https://forums.phpfreaks.com/topic/246158-preg_match-help/#findComment-1264255 Share on other sites More sharing options...
salathe Posted September 1, 2011 Share Posted September 1, 2011 congrats THANK YOU! Quote Link to comment https://forums.phpfreaks.com/topic/246158-preg_match-help/#findComment-1264264 Share on other sites More sharing options...
AyKay47 Posted September 1, 2011 Share Posted September 1, 2011 congrats THANK YOU! Quote Link to comment https://forums.phpfreaks.com/topic/246158-preg_match-help/#findComment-1264270 Share on other sites More sharing options...
salathe Posted September 1, 2011 Share Posted September 1, 2011 Not in public, m'dear. P.S. skwap, you need to swap the $mid and $url positions in the preg_match() call. Just to be sure you know what the problem is. Quote Link to comment https://forums.phpfreaks.com/topic/246158-preg_match-help/#findComment-1264274 Share on other sites More sharing options...
joe92 Posted September 1, 2011 Share Posted September 1, 2011 If the value of n is always going to be a number, you might as well put that in your pattern. Also, can you ever have a page where the value of n is nothing? If not, that may as well be in your pattern by using a plus instead of a star. preg_match('/\index.php?n=([0-9]+?)&img/si',$url, $mid); //assuming n is always a number P.s. I think you had the $url and the $mid the wrong way round too Quote Link to comment https://forums.phpfreaks.com/topic/246158-preg_match-help/#findComment-1264340 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.