2

Didn't see an answer that I could use so I'm going to ask.

I have a query string that I've broken into an array of values by exploding on the "&" sign. So, I now have an array containing strings such as:

email=my_name@my_domain.com
name=Michael+D+Linear
name=Michael%20Linear

and numerous other strings as well.

What I need to be able to do is search through the array and find the "email" strings and the "name" strings and place the actual email address into a new variable, the first name into a variable and the last name into a variable.

Have no idea how to do it effectively.

The name will always be displayed in either the first or the second fashion - never both. So, one time the query string might contain the name in the First+Middle+Last format (might not have a middle name, but all portions of the name will be divided with a + sign instead of a space). Other times the name would have the spaces replaced with the %20 sign. Never both in the same query.

How do I search through the array, find the email and name strings and then extract them to new variables?

Thanks for any assistance. Been wracking my brain all morning on this. Can't quite get it.

2
  • Do you really have two "name" strings? Commented May 2, 2013 at 11:24
  • Are you trying to decode a $_POST array? Commented May 2, 2013 at 11:25

3 Answers 3

2

Of course you could reinvent the wheel but you are clearly working with a query string:

<?php
  $str = 'email=my_name@my_domain.com&name=Michael%20Linear';

  parse_str($str, $arr);
  print_r($arr);

output:

Array
(
    [email] => my_name@my_domain.com
    [name] => Michael Linear
)
Sign up to request clarification or add additional context in comments.

5 Comments

nice one, the only reason I didn't mention this is because he seems to have the same key twice. If that's intentional (shouldn't be), parse_str() will only keep the last one.
@hexblot Figured but worth pointing out - either OP has incorrect query string or otherwise I'm working on the assumption that the name key has been correctly encoded as an array in the first place.
It would seem the post has been updated - so only 1 name appears at any time?
I'll also just add that irrespective of whether spaces are encoded with %20 or + they will both decode to a white space. If you need to separate a name - then you can play around with explode :)
As it turned out, in the end, this is the solution that worked for me in the simplest fashion. I tried to explain in original post that I won't have TWO name keys in my query string, only that the name key could come through in either of those two forms, and I needed to account for both. I didn't explain very well.
2

Simply exploding again around the = character would allow you to loop and check for the variable name.

Example :

foreach($q as $item) {
  $keyval = explode('=', $item);
  switch($keyval[0]) {
    case 'name':
      // Do something, $keyval[1] is the value part
      $name = urldecode($keyval[1]);
      break;
    case 'email':

      break;
  }
}

Note: + and %20 characters are replaced by spaces using the urldecode() function.

2 Comments

Looking at his data example, there will be two keys "name" and he will lose the first one by using explode();
that's actually something he needs to implement inside case 'name'. The code should overwrite $keyval. I've only added the name= part to demonstrate decoding special characters used for transport.
0

try using str_replace() replace string with your array index values

echo $email =str_replace('email=', '','email=my_name@my_domain.com');
 if(strpos('name=Michael+D+Linear', '+') !== false) {
 $f = str_replace('name=', '','name=Michael+D+Linear');
echo $fname = str_replace('+', ' ',$f);
}
 if(strpos('name=Michael%20Linear', '%20') !== false) {
 $g =str_replace('name=', '','name=Michael%20Linear');
echo $name = str_replace('%20', ' ',$g);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.