如何在php中使用regexp从字符串中提取用户名

far*_*oft 0 javascript php regex extract match

如何在php中使用regexp从字符串中提取用户名

<?php

$myString = "user=anas fares,pass=245654323456543dfgf345rgghf5y";

//result should be :

$username = "anas fares";
$password = "245654323456543dfgf345rgghf5y";

//remember, I dont know if username is anas fares or anything else.

?>
Run Code Online (Sandbox Code Playgroud)

Lek*_*eyn 5

if(preg_match('/^user=(.+?),pass=(.+?)$/', $myString, $matches)){// if there's no match, this won't be executed
   $username = $matches[1];
   $password = $matches[2];
}
Run Code Online (Sandbox Code Playgroud)

如果您知道允许使用哪些字符,建议使用字符类或..例如,您知道密码只包含字符az(仅小写)和0-9,您应该替换第二个(.+?)[a-z0-9]+?.如果你甚至知道它是一个固定的长度,如果是MD5哈希,你应该替换+?{32}(md5的固定长度为32)