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

标签 php mysql

我有一个 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/

相关文章:

Mysql - 计算两个字符串之间的soundex差异

python - 在 Django 1.7 上使用表单和 View 更新模型字段

PHP PDO 更新查询在执行时影响所有行

php - htmlspecialchars() 应该用于输入信息还是输出之前?

php - 私信系统查询

php - 如何在 1 个查询中执行?

php - 服务器找不到文件路径

javascript - 代码看起来不错,但是表单提交没有将 <canvas> 发布到 php

mysql - 创建一个触发器来插入一些内容,如果

mysql - 从 mysql 查询中获取当前 session /进程 ID