Extract String from a PHP Array

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.

Answers


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
)

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.


Need Your Help

Meteor xolvio:md-blog: Site title is changed to “name”

meteor

I have finally successfully installed the Meteor package md-blog from xolvio. The only thing that is bothering me, is that the title of the site is changed to "name". Where can I remove this?

Starting and stopping services on remote Linux servers

php linux ssh

I am in the middle of creating a game server control panel and i have a few issues.