0
id:10 Ivysaur Level:5

stored in side $_POST[A]

I'm trying just to grab the id (so the 10) and the name (which in this case is Ivysaur) and I want to store them in a session variable. But before I store them I'm trying to explode it so can split it into parts

$phoneChunks = explode("-", $_POST[A]);
echo "Raw Phone Number = $rawPhoneNumber <br />";
echo "First chunk = $phoneChunks[0]<br />";
echo "Second chunk = $phoneChunks[1]<br />";
echo "Third Chunk chunk = $phoneChunks[2]";

I am getting

Raw Phone Number =
First chunk = Id:10 Ivysaur Level:5
Second chunk =
Third Chunk chunk = 

What am i doing wrong ???

hakre
  • 193,403
  • 52
  • 435
  • 836

4 Answers4

2
preg_match("/id:(\d+) (.+?) Level:(\d+)/",$_POST['A'],$match);
list(,$id,$name,$level) = $match; // don't forget extra , at beginning of list!

Now you have the variables $id, $name and $level to do whatever you want with. Good luck from there :)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

You are trying to explode on a dash when there is no dash in your string. Try this:

$phoneChunks = explode(" ", $_POST[A]);
SBerg413
  • 14,515
  • 6
  • 62
  • 88
-1

Try this:

$chunk = explode(" ", $_POST['A']);

it should produce something like this:

Array
(
    [0] => id:10
    [1] => Ivysaur
    [2] => Level:5
)
ariefbayu
  • 21,849
  • 12
  • 71
  • 92
  • @Johan: I don't really like escaping data early on. I will always escape it RIGHT before sending it to database or processing it further that require escaping or XSRF. – ariefbayu Oct 06 '11 at 11:37
  • In the OP's code, the very next statement is an echo, you don't have the option to escape it at a later time. – Johan Oct 06 '11 at 11:49
  • if you use OP's question, I agree that I SHOULD escape it. But, echo is not what OP want. He/she want to store it in SESSION. – ariefbayu Oct 06 '11 at 12:59
-1

What am i doing wrong ???

You are not escaping $_POST['A']

$phoneChunks = explode(' ',hmtlentities($_POST['A']));

See: What are the best practices for avoiding xss attacks in a PHP site

Community
  • 1
  • 1
Johan
  • 74,508
  • 24
  • 191
  • 319