所以我有這段代碼,我需要決議字串中的 PHP 變數。這$string是來自 API 的回應,我無法控制輸出。我需要回顯它并將字串中的變數值顯示到 HTML 頁面。
<?php
$product = "Test Product";
$lot_number = "123123123";
$string = '<table style="width: 100.378%;" border="\">
<tbody>
<tr>
<td style="width: 10.4834%;"><strong>Product</strong></td>
<td style="width: 35.6575%;">$lot_number</td>
<td style="width: 11.9257%;"><strong>Lot No.</strong></td>
<td style="width: 41.9335%;">$lot_number</td>
</tr>
</tbody>
</table>';
echo "$string";
?>
這是這段代碼的輸出

這就是我想要達到的輸出。

知道怎么做嗎?謝謝!
uj5u.com熱心網友回復:
使第一個單引號'加倍",反之亦然。
<?php
$product = "Test Product";
$lot_number = "123123123";
$string = "<table style='width: 100.378%;' border='\'>
<tbody>
<tr>
<td style='width: 10.4834%;'><strong>Product</strong></td>
<td style='width: 35.6575%;''> $product </td>
<td style='width: 11.9257%;'><strong>Lot No.</strong></td>
<td style='width: 41.9335%;''> $lot_number</td>
</tr>
</tbody>
</table>";
echo $string;
?>
uj5u.com熱心網友回復:
多種方法來做到這一點。在您當前的情況下,最簡單的方法是關閉并重新打開您將列印 php 變數的部分。喜歡"abc " . $var . " def";
<?php
$product = "Test Product";
$lot_number = "123123123";
$string = '<table style="width: 100.378%;" border="\">
<tbody>
<tr>
<td style="width: 10.4834%;"><strong>Product</strong></td>
<td style="width: 35.6575%;">'. $product . '</td>
<td style="width: 11.9257%;"><strong>Lot No.</strong></td>
<td style="width: 41.9335%;">' . $lot_number .'</td>
</tr>
</tbody>
</table>';
echo "$string";
?>
更新
然后,您可以使用str_replace( https://www.php.net/manual/de/function.str-replace.php ) 函式來填充變數。
<?php
$product = "Test Product";
$lot_number = "123123123";
$string = '<table style="width: 100.378%;" border="\">
<tbody>
<tr>
<td style="width: 10.4834%;"><strong>Product</strong></td>
<td style="width: 35.6575%;"> $product </td>
<td style="width: 11.9257%;"><strong>Lot No.</strong></td>
<td style="width: 41.9335%;"> $lot_number </td>
</tr>
</tbody>
</table>';
#echo "$string";
$string = str_replace('$product', $product, $string);
$string = str_replace('$lot_number' , $lot_number , $string);
echo $string;
uj5u.com熱心網友回復:
如果您只有兩個變數,請使用以下代碼:
<?php
$product = "Test Product";
$lot_number = "123123123";
$stringFromApi = '<table style="width: 100.378%;">
<tbody>
<tr>
<td style="width: 10.4834%;"><strong>Product</strong></td>
<td style="width: 35.6575%;">$lot_number</td>
<td style="width: 11.9257%;"><strong>Lot No.</strong></td>
<td style="width: 41.9335%;">$lot_number</td>
</tr>
</tbody>
</table>';
$search = ['$product', '$lot_number'];
$replace = [$product, $lot_number];
$string = str_replace($search, $replace, $stringFromApi);
echo $string;
uj5u.com熱心網友回復:
由于您使用單引號,因此您無法在字串中添加變數,除非您使用花括號或點。
注意:使用雙引號方法比使用花括號或點要快。
如果要回顯一個變數并在 php 中使用單引號字串,則必須將這些部分“粘合”在一起。
有兩種方法可以做到這一點,它們都同樣快,這只是個人喜好問題,你想使用什么。
<?php
//Double quotations
$car = "mercedes";
echo "the car brand is a $car<br>";
$type = "Question";
echo "Hi! I just posted a {$type}<br>";
//Single quotations
$name = "StackOverFlow";
echo 'Hi! my name is: ' . $name;
?>
uj5u.com熱心網友回復:
它對你有用。有兩個字串運算子。第一個是連接運算子 ('.'),它回傳其左右引數的連接。
<?php
$product = "Test Product";
$lot_number = "123123123";
$string = '<table style="width: 100.378%;" border="\">
<tbody>
<tr>
<td style="width: 10.4834%;"><strong>Product</strong></td>
<td style="width: 35.6575%;">'.$product.'</td>
<td style="width: 11.9257%;"><strong>Lot No.</strong></td>
<td style="width: 41.9335%;">'.$lot_number.'</td>
</tr>
</tbody>
</table>';
echo $string;
?>
欲了解更多資訊:https ://www.php.net/manual/en/language.operators.string.php 。例子
<?php
$a = '12345';
// This works:
echo "qwe{$a}rty"; // qwe12345rty, using braces
echo "qwe" . $a . "rty"; // qwe12345rty, concatenation used
// Does not work:
echo 'qwe{$a}rty'; // qwe{$a}rty, single quotes are not parsed
echo "qwe$arty"; // qwe, because $a became $arty, which is undefined
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/441390.html
標籤:php
上一篇:PHP子類需要另一個子類
