Good day, the problem was born: There is a variable:

$Params = "login=Admin;pass:123123"; 

And now the main thing is to get the Admin value and put it into the $ user variable, and get the 123123 value, but put it into the $ password variable.

Conditions:

  1. Cannot use array
  2. Admin and 123123 change, tobish for each user his, I think it is clear

Thanks for attention :)

  • @IamS, According to the rules of the forum, questions should not be limited to the decision or the completion of student assignments. Please clarify what you have done yourself and what did not work out. Regular expressions to help. - Sergiks
  • @sergiks, that thing is that I have no idea how to do this, hoping that at least you can tell me where you can read how to solve this problem. - k0mar
  • one
    @IamS: Where did the requirement to not use arrays come from? Do you want to go or check? That is, parsing this format manually is not difficult, but why ? - VladD
  • and what if in the direction of csv, json? - Andrei Talanin
  • Hint: regular expressions . We'll have to read a little to enter the topic . Something like this: $ pattern = "/login=([^;;++);pass:(.+)/"; if (preg_match ($ pattern, $ Params, $ matches)) {$ user = $ matches [1]; $ password = $ matches [2]; printf ('Login:% s \ tpassword:% s \ n', $ user, $ password); } else {echo "Do not hit with the pattern .."; } - Sergiks

3 answers 3

Without an array - some kind of "Monsieur knows a lot .."

 $Params = "login=Admin;pass:123123"; list( $user, $password) = explode(';', preg_replace( "/login=([^;]+);pass:(.+)/", "$1;$2", $Params)); 

But there is still an array, although not visible.

And you can just delete all the extra two times :)

Upd. : without arrays, pure waste.

 $Params = "login=Admin;pass:123123"; $user = preg_replace( '/^login=([^;]+).+/', '$1', $Params); $password = preg_replace( '/^[^;]+;pass:(.+)/', '$1', $Params); 

Upd.2 : without regular expressions. It will break if the password or login contains the string " ;pass= ", which is unlikely, but possible.

 $Params = "login=Admin;pass:123123"; parse_str( str_replace( ';pass:', '&password=', $Params)); $user = $login; printf( "Логин: %s пароль: %s\n", $user, $password); 
  • one
    No, no, clean waste is a character to go on the string :) - user6550
  • @sergiks, yes, it works thanks) But only it does not find the password, login = qweqwe; password = 123 and writes it down, but the login finds and remembers correctly - k0mar

Cannot use array

I observe an excess concentration of free time.

http://ideone.com/DbwXXg

    Dear, do you happen to brush your teeth through your ear?

    a) explode 3 times. 1 time separator; . 2 times separator =. 3: b) preg_match

    In both cases, the array is used. Most interesting how to do without arrays.

    UPD : Here is the solution without arrays:

     $Params = "login=Admin;pass:123123"; $string = strpos($Params, ";"); $one = substr($Params, 0, $string); $oneString = strpos($one,'='); $login = substr($one, $oneString + 1); $two = substr($Params,$string + 1); $twoString = strpos($two,':'); $password = substr($two, $twoString + 1); echo 'login = '.$login.' password = '.$password; 

    Result of performance:

     login = Admin password = 123123 

    .