如何使用带符号和字符的php sscanf
我使用php函数sscanf解析字符串和extrac参数 此代码:如何使用带符号和字符的php sscanf,php,string,escaping,scanf,ampersand,Php,String,Escaping,Scanf,Ampersand,我使用php函数sscanf解析字符串和extrac参数 此代码: $s='myparam1=hello&myparam2=world'; sscanf($s, 'myparam1=%s&myparam2=%s', $s1, $s2); var_dump($s1, $s2); 显示: string(20) "hello&myparam2=world" NULL 但是我想要$s1的stringhello和$s2的strinworld 有什么帮助吗?%s不等同于regex
$s='myparam1=hello&myparam2=world';
sscanf($s, 'myparam1=%s&myparam2=%s', $s1, $s2);
var_dump($s1, $s2);
显示:
string(20) "hello&myparam2=world" NULL
但是我想要$s1的stringhello和$s2的strinworld
有什么帮助吗?
%s
不等同于regexp中的\w
:它不会只拾取字母数字
$s='myparam1=hello&myparam2=world';
sscanf($s, 'myparam1=%[^&]&myparam2=%s', $s1, $s2);
var_dump($s1, $s2);
但在这种情况下,使用可能是更好的选择
$s='myparam1=hello&myparam2=world';
parse_str($s, $sargs);
var_dump($sargs['myparam1'], $sargs['myparam2']);
使用如何
以下是一个例子:
$string = 'myparam1=hello&myparam2=world';
// Will use exactly the same format
preg_match('/myparam1=(.*)&myparam2=(.*)/', $string, $matches);
var_dump($matches); // Here ignore first result
echo("<br /><br />");
// Will match two values no matter of the param name
preg_match('/.*=(.*)&.*=(.*)/', $string, $matches);
var_dump($matches); // Here ignore first result too
echo("<br /><br />");
// Will match all values no matter of the param name
preg_match('/=([^&]*)/', $string, $matches);
var_dump($matches);
$string='myparam1=hello&myparam2=world';
//将使用完全相同的格式
preg_match('/myparam1=(.*)&myparam2=(.*)/',$string,$matches);
变量转储($matches);//这里忽略第一个结果
回声(“
”);
//将匹配两个值,无论参数名称如何
preg_match('/.*=(.*)&.*=(.*)/',$string,$matches);
变量转储($matches);//这里也忽略第一个结果
回声(“
”);
//将匹配所有值,无论参数名称如何
preg_match('/=([^&]*)/',$string,$matches);
var_dump($matches);
在所有三种情况下,matches数组都将包含参数
我很确信这样更好。
祝你好运 回答得很好。你说得对,我应该使用parse_str函数。非常感谢。