cheechm Posted August 5, 2008 Share Posted August 5, 2008 Hi, I have this string: /server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting&1217980250839 (and many more like it). I was wondering how I could get "Lighting". I specifically want it so that it looks for "Lighting" after the = sign after "category". Ie, like $_GET['category'], just I can't do that. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/ Share on other sites More sharing options...
markjoe Posted August 6, 2008 Share Posted August 6, 2008 http://us.php.net/manual/en/function.preg-match.php preg_match("/category=(.*)\b/", $stringVar, $matches); $matches[1]; $matches[1] will be "Lighting" This is off the top of my head (not sure if I used word boundry right), but there is the man page link also. It's at least a start. Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-609077 Share on other sites More sharing options...
cheechm Posted August 6, 2008 Author Share Posted August 6, 2008 Almost! Althought it includes the bit after lighting. Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-609096 Share on other sites More sharing options...
discomatt Posted August 6, 2008 Share Posted August 6, 2008 I think you want this guy http://php.net/manual/en/function.parse-str.php Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-609155 Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Yeah, I was just about to suggest that. Try: <?php $str = "/server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting&1217980250839"; $str = substr($str, strpos($str, '?')); parse_str($str, $output); echo $output['category']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-609157 Share on other sites More sharing options...
discomatt Posted August 6, 2008 Share Posted August 6, 2008 You can use strstr to isolate just the query as well <?php # Untested $str = '/server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting&1217980250839'; $query = substr( strstr($str, '?'), 1 ); parse_str( $query, $query_array ); print_r( $query_array ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-609159 Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Woops, forgot to take care of the ?: <?php $str = "/server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting&1217980250839"; $str = substr($str, strpos($str, '?') + 1); parse_str($str, $output); echo $output['category']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-609167 Share on other sites More sharing options...
.josh Posted August 6, 2008 Share Posted August 6, 2008 here's my take: <?php $url = "/server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting&1217980250839"; $url_query = parse_url($url); parse_str($url_query['query']); echo $category; ?> Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-609170 Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Any of the 3 work. xD There's like, a million ways to accomplish any particular task in PHP. >_< Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-609172 Share on other sites More sharing options...
discomatt Posted August 6, 2008 Share Posted August 6, 2008 here's my take: <?php $url = "/server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting&1217980250839"; $url_query = parse_url($url); parse_str($url_query['query']); echo $category; ?> This way will create an un-needed array, and generally take more processing power than basic string comparisons... but yes, it will work Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-609308 Share on other sites More sharing options...
cheechm Posted August 6, 2008 Author Share Posted August 6, 2008 Thanks very much. Solved! Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-609397 Share on other sites More sharing options...
.josh Posted August 6, 2008 Share Posted August 6, 2008 here's my take: <?php $url = "/server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting&1217980250839"; $url_query = parse_url($url); parse_str($url_query['query']); echo $category; ?> This way will create an un-needed array, and generally take more processing power than basic string comparisons... but yes, it will work You know, according to the manual, parse_url is supposed to accept a second argument to only return the part you want (as a string), but when I tried to add PHP_URL_QUERY as the 2nd argument, php kept screaming at me about how parse_url only accepts one argument...so...am I just not understanding the manual right, or is the manual wrong? edit: Oh wait, I just read further down: the component argument is a feature of php5 I was testing it on a php4 server. whoops Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-609780 Share on other sites More sharing options...
cheechm Posted August 6, 2008 Author Share Posted August 6, 2008 I am using this one: <?php $str = "/server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting&1217980250839"; $str = substr($str, strpos($str, '?') + 1); parse_str($str, $output); echo $output['category']; ?> However, the plus sign doesn't seem to work. If the string contains a plus sign, for instance "Lighting+", then the plus doesn't show. Please help! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-610212 Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Give me an example string please. Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-610214 Share on other sites More sharing options...
cheechm Posted August 6, 2008 Author Share Posted August 6, 2008 /server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting+&1217980250839 Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-610219 Share on other sites More sharing options...
Barand Posted August 6, 2008 Share Posted August 6, 2008 a plus sign in a URL represents a space. Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-610232 Share on other sites More sharing options...
cheechm Posted August 6, 2008 Author Share Posted August 6, 2008 But it is a string now. The string contains the plus sign. Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-610235 Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Yes, but parse_str() assumes a URL. Try urlencode()'ing the output? Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-610238 Share on other sites More sharing options...
discomatt Posted August 6, 2008 Share Posted August 6, 2008 Plus signs are converted to ' ' by parse_str. urlencode will conver it to it's hex equivalent (%2B)... In order to get the exact string-to-string conversion, you'll have to urlencode the string before parse_str, and urldecode after... Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-610262 Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Yeah, that's what I meant. >_< Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-610263 Share on other sites More sharing options...
cheechm Posted August 7, 2008 Author Share Posted August 7, 2008 I tried that and it didn't seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-610488 Share on other sites More sharing options...
cheechm Posted August 11, 2008 Author Share Posted August 11, 2008 Bump Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-614098 Share on other sites More sharing options...
cheechm Posted August 12, 2008 Author Share Posted August 12, 2008 Bumpity bump. Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-614352 Share on other sites More sharing options...
discomatt Posted August 12, 2008 Share Posted August 12, 2008 You know what? Just use regex <?php $subject = '/server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting+&1217980250839'; preg_match('/category=([^&]++)/', $subject, $match ); echo $match[1]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-614433 Share on other sites More sharing options...
sasa Posted August 12, 2008 Share Posted August 12, 2008 wrong post Quote Link to comment https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/#findComment-614583 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.