How to get a specific variable value

My database tables 1 row looks like this:

enter image description here

I save all the variables that are used in the body in another column called vars . Then removing all the vars. If they are multiples, they explode with the symbol ",".

What I want to do is extract all used vars from db, and then filter them out with a second filter function. As you can see, the filter has 3 inputs: $content, $needle, $replacement .

$ content - message body,

$ needle - % . What are we looking for. %

$ replacement is the var we got from the database defined by php. For example, if I get wsurl from a database, it is already defined in my settings.php, for example DEFINE ("wsurl", "www.yourdomain.com").

The question is, how can I send a specific value as the third input - $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); } 
+6
source share
1 answer

If I understood the question correctly. This should help.

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

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

+13
source

Source: https://habr.com/ru/post/906364/


All Articles