jamesmiller Posted September 8, 2011 Share Posted September 8, 2011 hey guys this should be a simple one but im struggling, i have the regex :- preg_match_all('!/search/(.*)/([0-9])!i', $page, $pages); and that works for the numbers between 1-9 but how would i change ([0-9]) to match any number? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/246708-help-with-preg_match_allsearch0-9i-page-pages/ Share on other sites More sharing options...
ManiacDan Posted September 8, 2011 Share Posted September 8, 2011 Regular expressions have "modifiers" that alter the number of items a specific block can match. Putting a plus after something means "one or more times." So if you did [0-9]+ that would match any number of characters. Regular expressions also have built in character classes. \d means "any digit," so you can replace [0-9]+ with \d+ and get the same results, plus many people think it's more readable. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/246708-help-with-preg_match_allsearch0-9i-page-pages/#findComment-1266861 Share on other sites More sharing options...
jamesmiller Posted September 8, 2011 Author Share Posted September 8, 2011 Dan, Thank you, i have tried putting \d+ in the regex but it didnt work so tried (d+) and worked :-) thank you for the help Quote Link to comment https://forums.phpfreaks.com/topic/246708-help-with-preg_match_allsearch0-9i-page-pages/#findComment-1266874 Share on other sites More sharing options...
jamesmiller Posted September 8, 2011 Author Share Posted September 8, 2011 Ok new problem its outputting: Array ( [0] => Array ( [0] => href="/search/reg/2/">2 3 4 5 ... 32 Array ( [0] => reg/2/">2 3 4 5 ... 32< ) [2] => Array ( [0] => d ) ) The thing is i want /search/reg/number/ to appear in different array places so /search/reg/3/ would be in array value 1 and /search/reg/4/ would be in value 2 Quote Link to comment https://forums.phpfreaks.com/topic/246708-help-with-preg_match_allsearch0-9i-page-pages/#findComment-1266876 Share on other sites More sharing options...
ManiacDan Posted September 8, 2011 Share Posted September 8, 2011 The parentheses determine the "capture group." $matches[0] will be the whole pattern. $matches[1] will be the first set of parens, $matches[2] will be the second set, etc. What you've posted is too poorly formatted for me to tell what exactly is going on. Use [ code ] tags around your output so we can see it better. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/246708-help-with-preg_match_allsearch0-9i-page-pages/#findComment-1266924 Share on other sites More sharing options...
jamesmiller Posted September 8, 2011 Author Share Posted September 8, 2011 My apologies, thank you , Array ( [0] => Array ( [0] => href="/search/reg/2/">2 3 4 5 ... 32 Array ( [0] => reg/2/">2 3 4 5 ... 32< ) [2] => Array ( [0] => d ) ) That was the output Quote Link to comment https://forums.phpfreaks.com/topic/246708-help-with-preg_match_allsearch0-9i-page-pages/#findComment-1266929 Share on other sites More sharing options...
ManiacDan Posted September 8, 2011 Share Posted September 8, 2011 This isn't valid output from a print_r statement, there appears to be a great amount of this array missing. Are you using preg_match or preg_match_all? If you're using preg_match_all then your result will be an array of arrays of matches like I described. Show some more code so we can tell exactly what's up here. Maybe a little sample app Quote Link to comment https://forums.phpfreaks.com/topic/246708-help-with-preg_match_allsearch0-9i-page-pages/#findComment-1266934 Share on other sites More sharing options...
jamesmiller Posted September 8, 2011 Author Share Posted September 8, 2011 here is my code : function page($page) { preg_match_all('!href="/search/(.*)/(d+)!i', $page, $pages); print_r ($pages); Thank you Quote Link to comment https://forums.phpfreaks.com/topic/246708-help-with-preg_match_allsearch0-9i-page-pages/#findComment-1266936 Share on other sites More sharing options...
ManiacDan Posted September 8, 2011 Share Posted September 8, 2011 \d, not d. Also, your .* is far too greedy, you want something like this: function page($page) { preg_match_all('!href="/search/(.*?)/(\d+)!i', $page, $pages); print_r ($pages); Once successfully matched, you'll need to loop through $pages to get arrays of matches: foreach ( $pages as $match ) { echo "{$match[1]} .. {$match[2]}<br />\n"; } -Dan Quote Link to comment https://forums.phpfreaks.com/topic/246708-help-with-preg_match_allsearch0-9i-page-pages/#findComment-1266944 Share on other sites More sharing options...
jamesmiller Posted September 8, 2011 Author Share Posted September 8, 2011 Than you works perfectly , thank you for all your help dan Quote Link to comment https://forums.phpfreaks.com/topic/246708-help-with-preg_match_allsearch0-9i-page-pages/#findComment-1266958 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.