解析php中的间隔字符串

时间:2013-05-29 05:10:14

标签: php username

伙计们,我使用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
                );
}

?>

2 个答案:

答案 0 :(得分:0)

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

OR

使用以下样式代码:

function ParsePost( )
{

    //$post = "Username&Password";

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

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

    return array( 'user' => $pieces[0],
              'pass' => $pieces[1]
                );
}

答案 1 :(得分:-1)

只需添加:

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

在:

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

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

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

这也将_替换为空格。

实现此目的的最佳方法实际上是使用爆炸。

list($username, $password) = explode('$', $post);
相关问题