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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.