Javascript 在php中提取字符串中以$开头的单词
我想对文本区域内容中以字符$开头的单词进行分组。这是编辑器的内容示例段落: Hi $firstname, Please find the company credentials mentioned below: Employee Name: Employee ID: Employee username: $username Email ID: $email Password: $password Regards, Administration Team 输出为:Javascript 在php中提取字符串中以$开头的单词,javascript,php,mysqli,Javascript,Php,Mysqli,我想对文本区域内容中以字符$开头的单词进行分组。这是编辑器的内容示例段落: Hi $firstname, Please find the company credentials mentioned below: Employee Name: Employee ID: Employee username: $username Email ID: $email Password: $password Regards, Administration Team 输出为: Array (
Array (
[0] => $firstname
[1] => $
)
我需要输出为:
Array (
[0] => $firstname
[1] => $username
[2]=>$email
[3]=>$password
)
我做错了什么?您需要使用
preg\u match\u all
<代码>预匹配仅返回第一个匹配。此外,请修复regexp以匹配实际文本:
$pattern = '/[$](\w+)/';
preg_match_all($pattern, $input, $matches);
foreach($matches as $match) {
echo $match[0] . ': ' . $match[1];
}
这将输出:
$firstname : firstname
$username : username
$email : email
$password : password
[$]
周围不需要括号。他们负责您的结果中的$matches[1]
。preg\u match\u all()
是获取所有匹配项所需的。顺便问一下,您对匹配项做了什么?你在做什么替换吗?
$firstname : firstname
$username : username
$email : email
$password : password
$matches = array();
preg_match_all('/\$\w+/', $text, $matches);
print_r($matches);