我有一个 PHP 字符串,其中包含以下代码:
<html>
<head>
<body>
<p>Hi! $username$, your email is $email$</p>
</body>
</head>
</html>
它存储在数据库中,我有很多。 $ 之间的变量在每个记录中可以不同。我想获取变量的名称并将它们存储到数组中,例如:
array('username', 'email');
谢谢
编辑:文档 HTML 是动态的并存储在数据库中,例如我也可以有这样的东西:
<span>Test of $user$ with the $test$ and $foo$ and also a $foo2$ var</span>
最佳答案
您可以使用preg_match_all
函数。
尝试下面的代码:
$html = '<html>
<head>
<body>
<p>Hi! $username$, your email is $email$</p>
</body>
</head>
</html>';
preg_match_all("/\\$(.*?)\\$/", $html, $matches1);
$required_array = $matches1[1];
print_r($required_array);
输出
Array
(
[0] => username
[1] => email
)
关于php - PHP中按分隔符搜索单词并将其放入数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36469175/