-1

伙计们,我使用 Parse Post 接收我的用户名和密码但是当我的用户名包含在 Space 中时它不起作用。我的问题是如何在我的 php 代码中解析空格字符串?

<?php
function ParsePost( )
{
    $username = '';
    $password = '';

    $post = file_get_contents( "php://input" );

    $post = str_replace( "&", " ", $post );

    sscanf( $post, "%s  %s", $username, $password );

    return array( 'user' => $username,
              'pass' => $password
                );
}

?>
4

2 回答 2

0

你可以使用 sscanf( $post, "%s&%s", $username, $password );

或者

使用以下样式代码:

function ParsePost( )
{

    //$post = "Username&Password";

    $post = file_get_contents( "php://input" );

    $pieces = explode('&', $post);

    return array( 'user' => $pieces[0],
              'pass' => $pieces[1]
                );
}
于 2013-05-29T05:21:20.210 回答
-1

只需添加:

$post = str_replace( " ", "_", $post );

前:

$post = str_replace( "&", " ", $post );

用户名将以_s 生成,因此您可能希望在返回之前将它们转换回空格:

$username = str_replace( "_", " ", $username);

这也将替换_为空格。

做到这一点的最好方法实际上是使用爆炸。

list($username, $password) = explode('$', $post);
于 2013-05-29T05:13:11.750 回答