亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

來自 db 的值包含 php 變量名 ('hello $name') ,如何正確顯示?

來自 db 的值包含 php 變量名 ('hello $name') ,如何正確顯示?

PHP
慕容森 2023-04-15 11:01:17
從 db 我得到一個包含 php 變量名稱的字符串,如 'hello $name' ,我不知道如何正確顯示它:例如:假設我有函數顯示名稱:function echoName($name){              //call to db to get string (hello $name)          $helloname = $row['helName'];          echo $helloname; //lets say name = jinko it should print hello jinko         //but it prints hello $name         // i have tried "$helloname"; it doesn't work}在我的例子中,數據庫包含:Il tuo codice di prenotazione è $codicePrenotazione<br>Ricordiamo la data : $newDate我的功能代碼:function sendEmail($cognome,$nome,$email,$codicePrenotazione,$date){ require './dbconnection.php';  $mail = new PHPMailer(true);  $query = 'SELECT Azienda_Nome,Azienda_email_notifiche_smtp,Azienda_email_notifiche_utente ,Azienda_email_notifiche_pwd,Azienda_email_notifiche_porta,Azienda_email_notifiche_sicurezza ,Azienda_mex_utente FROM `aziende`';  $stmt =$conn->query($query);  $stmt->execute();   $row = $stmt->fetch();   $siteOwnersEmail = $row['Azienda_email_notifiche_utente'];  $smtp =$row['Azienda_email_notifiche_smtp'];  $password =$row['Azienda_email_notifiche_pwd'];  $porta=strtolower( $row['Azienda_email_notifiche_porta']);  $sicurezza=strtolower( $row['Azienda_email_notifiche_sicurezza']);  $contact_message = $row['Azienda_mex_utente'];//this line contains the string   $nomeAzienda = $row['Azienda_Nome'];  $conn = null;  $newDate = date("d-m-Y H:i", strtotime($date));    $name = $nome.' '.$cognome;   $email = $email;   $subject = 'Codice Prenotazione';//etc etc }
查看完整描述

2 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

你最好使用類似的東西sprintf。

例如,如果你的數據庫中有這個:

Hello,?your?name?is?%s

你可以像這樣格式化它:

echo?sprintf($row["heName"],?"Test");

這將輸出:

Hello,?your?name?is?Test

如果你真的想使用 PHP 風格的變量名,一種方法是用preg_replace_callback它們各自的值替換變量:

<?php


function format_string($string, $variables) {

? ? return preg_replace_callback("~\\$([A-Za-z0-9-_]+)~",

? ? function($match) use (&$variables) {

? ? ? ? $varname = $match[1];


? ? ? ? return $variables[$varname];

? ? }, $string);

}


echo format_string("Hello \$user", /* variable table */ [

? ? "user" => "Test"

]);


查看完整回答
反對 回復 2023-04-15
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

您可以使用評估...

$name = "jinko";

$helloname = "hello $name";

eval("\$helloname = \"$helloname\";");


echo $helloname; // prints: hello jinko

警告:這可能很危險,因為 eval 會“執行”任何 PHP 代碼。因此,僅當來自數據庫的數據安全時才使用它。


查看完整回答
反對 回復 2023-04-15
  • 2 回答
  • 0 關注
  • 144 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號