gpt4 book ai didi

php - 在 PHP 中按分隔符搜索单词并将它们放入数组中

转载 作者:行者123 更新时间:2023-11-29 02:17:57 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com