爆炸的PHP帖子

时间:2011-10-06 11:07:15

标签: php mysql sql

id:10 Ivysaur Level:5

存储在$ _POST [A]

的旁边

我正在尝试抓住id(所以10)和名称(在本例中是Ivysaur)并且我想将它们存储在会话变量中。但在我存储它们之前,我试图将它爆炸,因此可以将它分成几部分

$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]";

我正在

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

我做错了什么???

4 个答案:

答案 0 :(得分:2)

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

现在,您可以使用变量$id$name$level来执行任何操作。祝你好运:)

答案 1 :(得分:0)

当你的字符串中没有短划线时,你试图在短划线上爆炸。试试这个:

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

答案 2 :(得分:-1)

试试这个:

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

它应该产生这样的东西:

Array
(
    [0] => id:10
    [1] => Ivysaur
    [2] => Level:5
)

答案 3 :(得分:-1)

  

我做错了什么???

您没有逃避$_POST['A']

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

请参阅:What are the best practices for avoiding xss attacks in a PHP site