使用css样式更改php代码的字体

时间:2015-09-04 00:34:51

标签: php html css forms switch-statement

代码:

df <- read.table(text = "ID1, X, 2
ID1, Y, 1
ID1, Z, 2
ID2, X, 1
ID2, Z, 1
ID3, A, 2", sep = ",", col.names=c("ID","Code","Value"))
library(tidyr)
spread(df,Code, Value )
## ID  A  X  Y  Z
## 1 ID1 NA  2  1  2
## 2 ID2 NA  1 NA  1
## 3 ID3  2 NA NA NA

我不知道如何更改&#39;名称&#39;的字体?从文本框输出。是否可以在PHP代码中使用CSS样式?我想更改从文本框输出的文本的font-weight和font-family。感谢

2 个答案:

答案 0 :(得分:1)

是的,您可以像对待任何立即应用的CSS样式一样:

$result = "<p style="font-weight:val;font-family:val;">Hello, {$_POST['name']}!</p>"; 

您可以根据需要更改这些值,并在echo出现时显示样式。

或者,如下所示,您可以使用CSS包装$result,或者如果您要设置不同的值,则可以在开关案例中应用它们,如下所示:

switch ($_POST['language']) {
case "option1":
$result = "Hello, {$_POST['name']}!"; 
$style = 'font-weight:val;font-family:val';
break;
case "option2":
$result = "你好, {$_POST['name']}!"; 
$style = 'font-weight:val;font-family:val';
break;
case "option3":
$result = "Bonjour, {$_POST['name']}!";
$style = 'font-weight:val;font-family:val';
break;
}
echo '<p style="' . $style . '">' . $result . '</p>';

答案 1 :(得分:0)

<!DOCTYPE html>
<html>
<head>
    <title>Greeting Service!</title>
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css' />
    <style type="text/css">
        #name {
            font-family: "Open Sans", sans-serif;
            font-weight: 700;
            background-color: red;
        }
        #greet {
            font-family: "Open Sans", sans-serif;
            font-weight: 400;
        }
    </style>
</head>
<body>
    <center>
        <?php
        if (isset($_POST['language'])) {
        $language = $_POST['language'];
        switch ($_POST['language']) {
        case "option1":
        $result = "<p id=\"name\">Hello, {$_POST['name']}!</p>"; 
        break;
        case "option2":
        $result = "<p id=\"name\">你好, {$_POST['name']}!</p>"; 
        break;
        case "option3":
        $result = "<p id=\"name\">Bonjour, {$_POST['name']}!</p>"; 
        break;
        }
        echo $result;
        } else {
        ?>
            <form method="post" action="">
            <h1 id="name">What's Your Name?</h1>
            <input type="text" name="name" placeholder="Name Here" />
            <h4 id="greet">Greet me in:
                <select name="language">
                    <option value="option1">English</option>
                    <option value="option2">Chinese</option>
                    <option value="option3">French</option>
                </select>
            </h4>
            <input type="submit" value="Greet Me!" />
            </form>
        <?php
        }
        ?>
    </center>
</body>
</html>