LiliVG Posted September 29, 2009 Share Posted September 29, 2009 This is how far I've gotten. So far, $page_code would print something like "read.php?1,2,2" $page_url = $_SERVER['HTTP_REFERER']; $parse = parse_url($page_url); $path = $parse['path']; $query = $parse['query']; $page_code = $path.$query; I want to cut off anything including and after the second comma, how would I do that so I end up with $page_code == "read.php?1,2" ? Quote Link to comment https://forums.phpfreaks.com/topic/175995-solved-how-to-count-the-number-of-commas-and-then-cut-off-after-2nd-case/ Share on other sites More sharing options...
abazoskib Posted September 29, 2009 Share Posted September 29, 2009 you can use the substr() method Quote Link to comment https://forums.phpfreaks.com/topic/175995-solved-how-to-count-the-number-of-commas-and-then-cut-off-after-2nd-case/#findComment-927359 Share on other sites More sharing options...
.josh Posted September 29, 2009 Share Posted September 29, 2009 $url = "http://www.somesite.com/read.php?1,2,2,4,5"; $url = basename($url); preg_match('~^[^,]+(?:,[^,]+)?~',$url,$match); echo $match[0]; Quote Link to comment https://forums.phpfreaks.com/topic/175995-solved-how-to-count-the-number-of-commas-and-then-cut-off-after-2nd-case/#findComment-927363 Share on other sites More sharing options...
ldb358 Posted September 29, 2009 Share Posted September 29, 2009 you could also use: $url = 'http://www.somesite.com/read.php?1,2,2,4,5'; $url = explode( ',' , $url); $final = $url['0'] . $url['1']; echo $final; Quote Link to comment https://forums.phpfreaks.com/topic/175995-solved-how-to-count-the-number-of-commas-and-then-cut-off-after-2nd-case/#findComment-927372 Share on other sites More sharing options...
LiliVG Posted September 30, 2009 Author Share Posted September 30, 2009 you could also use: $url = 'http://www.somesite.com/read.php?1,2,2,4,5'; $url = explode( ',' , $url); $final = $url['0'] . $url['1']; echo $final; This looks interesting, I think I'll give that a shot! Quote Link to comment https://forums.phpfreaks.com/topic/175995-solved-how-to-count-the-number-of-commas-and-then-cut-off-after-2nd-case/#findComment-927379 Share on other sites More sharing options...
LiliVG Posted September 30, 2009 Author Share Posted September 30, 2009 you could also use: $url = 'http://www.somesite.com/read.php?1,2,2,4,5'; $url = explode( ',' , $url); $final = $url['0'] . $url['1']; echo $final; This looks interesting, I think I'll give that a shot! Very close! It does *almost* what I need. I cuts off everything including and after the second comma, BUT, it removes the first comma, which I need... Quote Link to comment https://forums.phpfreaks.com/topic/175995-solved-how-to-count-the-number-of-commas-and-then-cut-off-after-2nd-case/#findComment-927380 Share on other sites More sharing options...
LiliVG Posted September 30, 2009 Author Share Posted September 30, 2009 Ok, I got it, I changed $final = $url['0'] . $url['1']; to $final = $url['0'] .','. $url['1']; and that works. Explode() seems to use the character as a divider, and removes it from the results, at least in the first case with 0 it does. Anyways, it works, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/175995-solved-how-to-count-the-number-of-commas-and-then-cut-off-after-2nd-case/#findComment-927388 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.