简体   繁体   English

将lang函数放在php字符串中

[英]Put a lang function inside of a php string

Trying to do a translation of my MySQL DB to PHP. 尝试将我的MySQL数据库转换为PHP。

Here's the code: 这是代码:

$sql = "SELECT Name, Price FROM utf WHERE Name LIKE 'K%'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<div class=\"CSSTableGenerator style=\"width:600px;height:150px;\"><table><tr><th>Name</th><th>Price</th></tr></div>";
    // output data of each row
   while($row = $result->fetch_assoc()) {
    $name = str_replace('K', 'Karambit', $row['Name']);
    echo "<tr><td>".$name."</td><td>".$row["Price"]." ".$row["Trend"]."</td></tr>";
}
   echo "</table>";

So Select, Filter by signature character, and then translate. 因此,请选择“按签名字符过滤”,然后进行翻译。

Now, I have a lang.php file which has the translations. 现在,我有一个包含翻译的lang.php文件。

Here it is: 这里是:

<?php
function lang($phrase){
    static $lang = array(
        'ba' => 'Bayonet',
        'ka' => 'Karambit'
    );
    return $lang[$phrase];
}
?>

How do I put it in this line: 我如何把它放在这一行:

$name = str_replace('K', 'Karambit', $row['Name']);

and replace 'ka' => 'Karambit' with 'Karambit' ? 并将'ka' => 'Karambit'替换为'Karambit'

Non of these have worked: 这些都不起作用:

//attempt 1
$name = str_replace('K', 'lang('ka');', $row['Name']);
//attempt 2
$name = str_replace('K', '$lang('ka');', $row['Name']);
//attempt 3
$word = echo lang('ka');
$name = str_replace('K', '$word', $row['Name']);

Don't put function calls in quotes, just use: 不要将函数调用放在引号中,只需使用:

$name = str_replace('K', lang('ka'), $row['Name']);
                       //^^^^^^^^^^ See here

Also to give you another option: 还给您另一个选择:

You can store your search => replace values into an array like this: 您可以将search => replace值存储到这样的数组中:

$searchReplace = [
    "ba" => "Bayonet",
    "ka" => "Karambit",
];

And then you can simply use strtr() to replace it, eg 然后您可以简单地使用strtr()替换它,例如

$name = strtr($row['Name'], $searchReplace);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM