如何通过变量获取定义的值

时间:2022-06-26 06:40:17

My database tables 1 row looks like that

我的数据库表1行看起来像那样

如何通过变量获取定义的值

I'm storing all variables that used in body, in another column named vars. Then fetching all vars. If they are multiple, exploding by "," symbol.

我正在存储在body中使用的所有变量,在另一个名为vars的列中。然后获取所有变量。如果它们是多个,则按“,”符号爆炸。

What I want to do is, fetch all used vars from db, then filter them by second function filter. As you see, filter has, 3 inputs: $content, $needle, $replacement .

我想要做的是,从db获取所有使用过的变量,然后通过第二个函数过滤器过滤它们。如你所见,过滤器有3个输入:$ content,$ needle,$ replacement。

$content - messages body,

$ content - 消息正文,

$needle - %.what we are searching for.%

$ needle - %。我们正在寻找什么。%

$replacement - is var that we got from database, defined by php. For ex, if I get wsurl from database, It's already defined in my settings.php like DEFINE("wsurl", "www.yourdomain.com").

$ replacement - 是我们从数据库获得的var,由php定义。例如,如果我从数据库获得wsurl,它已经在我的settings.php中定义,如DEFINE(“wsurl”,“www.yourdomain.com”)。

Question is, how can I send defined value as third input - $replacement?

问题是,如何将定义值作为第三个输入发送 - $ replacement?

public function genMsg($id) {
    $msgid = $id;
    $stmt = $db->prepare('SELECT `body`, `status`, `vars` FROM `messages` WHERE `id`=?') or die(htmlspecialchars($this->db->error));
    $stmt->bind_param("i", $msgid) or die(htmlspecialchars($stmt->error));
    $stmt->execute() or die(htmlspecialchars($stmt->error));
    $stmt->bind_result($message, $status, $vars) or die($stmt->error);
    $stmt->fetch() or die($stmt->error);
    $stmt->close();
    $image = ($status == -1) ? "fail" : "success";
    if (isset($vars) && !empty($vars)) {
        if (strpos($vars, ",")) {
            $vars = explode(",", $vars);
            foreach ($vars as $key => $var) {
                $this->filter($message, $var, <HERE MUST BE DEFINED VALUE>);
            }
        } else {
            $var = trim($vars);
            $this->filter($message, $var, <HERE MUST BE DEFINED VALUE>);
        }
    }
    $data = array(
        'status' => $status,
        'message' => $message
    );
}

protected function filter($content, $needle, $replacement) {
    return str_replace("%'.$needle.'%", $replacement, $content);
}

1 个解决方案

#1


13  

If I got your question right. This should help.

如果我的问题是正确的。这应该有所帮助。

<?php
    define('TEST', 'abc');
    echo constant('TEST');

http://www.php.net/manual/en/function.constant.php

#1


13  

If I got your question right. This should help.

如果我的问题是正确的。这应该有所帮助。

<?php
    define('TEST', 'abc');
    echo constant('TEST');

http://www.php.net/manual/en/function.constant.php