Jump to content

[SOLVED] Easy question


gtal3x

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/79649-solved-easy-question/
Share on other sites

/(.*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.

Link to comment
https://forums.phpfreaks.com/topic/79649-solved-easy-question/#findComment-403429
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/79649-solved-easy-question/#findComment-403636
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/79649-solved-easy-question/#findComment-403640
Share on other sites

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>";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/79649-solved-easy-question/#findComment-403650
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.