gtal3x Posted December 1, 2007 Share Posted December 1, 2007 hello i have got a very long variable wich i need to sort out into small ones (it contains ';' so i seperate them...) $var = "Some values here"; $var = split(";", $var); foreach($var as $value) { $user = stristr($value, 'somevalue='); echo $user; } This works fine however it echos somevalue=12345, how can i do so so it echos only the value between somevalue= >AND (print me!)< ; Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/79649-solved-easy-question/ Share on other sites More sharing options...
kenrbnsn Posted December 1, 2007 Share Posted December 1, 2007 Can you re-work your example to specify the string in question? As your example now read, it doesn't make any sense. Ken Quote Link to comment https://forums.phpfreaks.com/topic/79649-solved-easy-question/#findComment-403378 Share on other sites More sharing options...
PHP_PhREEEk Posted December 1, 2007 Share Posted December 1, 2007 I was wondering if you could be more cryptic? PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/79649-solved-easy-question/#findComment-403379 Share on other sites More sharing options...
BenInBlack Posted December 1, 2007 Share Posted December 1, 2007 /(.*somevalue=)([0-9]+)(;.*)/ $string = 'somevalue=12345;'; if (eregi('(.*somevalue=)([0-9]+)(;.*)', $string,$collector)) { echo $collector[1]; } Quote Link to comment https://forums.phpfreaks.com/topic/79649-solved-easy-question/#findComment-403382 Share on other sites More sharing options...
jumpfroggy Posted December 1, 2007 Share Posted December 1, 2007 /(.*somevalue=)([0-9]+)(;.*)/ $string = 'somevalue=12345;'; if (eregi('(.*somevalue=)([0-9]+)(;.*)', $string,$collector)) { echo $collector[1]; } I was going to say, do another split since you're already used to them: $var = "Some values here"; $var = split(";", $var); foreach($var as $value) { list($temp, $user) = split("=",$value); echo $user; } However, this only works for numbers. If you have a string, you could have a = in the string which screws everything up. BenInBlack's is more robust. Quote Link to comment https://forums.phpfreaks.com/topic/79649-solved-easy-question/#findComment-403429 Share on other sites More sharing options...
gtal3x Posted December 1, 2007 Author Share Posted December 1, 2007 Thanks for replys sorry if i got u confused with my variable, basicly the variable would be $information = "username=MyUsername; password=mypass; somethinelse=tralala; ....AND SO ON"; and basicly i wont to print out this variable into other variables example $username = "MyUsername"; so i wont first to clear the username= ; and get the value in between them... i tryed the solutions from here but they dident work, i know its my fualt i dident explain propely thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/79649-solved-easy-question/#findComment-403636 Share on other sites More sharing options...
wildteen88 Posted December 1, 2007 Share Posted December 1, 2007 Try: $information = "username=MyUsername; password=mypass; somethinelse=tralala"; $variables = explode('; ', $information); foreach($variables as $variable) { list($variable_name, $variable_value) = explode('=', $variable); $$variable_name = $variable_value; } echo '$username = ' . $username . '<br />'; echo '$password = ' . $password . '<br />'; echo '$somethinelse = ' . $somethinelse; Quote Link to comment https://forums.phpfreaks.com/topic/79649-solved-easy-question/#findComment-403640 Share on other sites More sharing options...
jumpfroggy Posted December 1, 2007 Share Posted December 1, 2007 This works form me: <?php $information = "username=MyUsername; password=mypass; somethinelse=tralala; ....AND SO ON"; // Splits the string into each "username=Myusername" section... $items = split( "; ", $information ); foreach ( $items as $item ) { // Find the position of the first occurance of the = sign $pos = strpos( $item, "=" ); // NOTE: Must use !== here, not just !=. If $pos is 0 (valid match on first character), then $pos==false is true but $pos===false not. if ( $pos !== false ) { $name = substr( $item, 0, $pos ); $value = substr( $item, $pos+1 ); print "name: $name <br>"; print "value: $value <br>"; print "<br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/79649-solved-easy-question/#findComment-403650 Share on other sites More sharing options...
gtal3x Posted December 1, 2007 Author Share Posted December 1, 2007 Thanks a lot to every1 !!! Quote Link to comment https://forums.phpfreaks.com/topic/79649-solved-easy-question/#findComment-403673 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.