如何设置 PHP 回显文本的样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18252433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:14:26  来源:igfitidea点击:

How can I style a PHP echo text?

phpcssstyling

提问by thomas

I have the following code;

我有以下代码;

<?php
    function countryCityFromIP($ipAddr)
    {
        $url = "http://api.ipinfodb.com/v3/ip-city/?key=5cfaab6c5af420b7b0f88d289571b990763e37b66761b2f053246f9db07ca913&ip=$ipAddr&format=json";
        $d = file_get_contents($url);
        return json_decode($d , true);
    }

    if (isset($_REQUEST['submit']))
    {
       $ip = countryCityFromIP($_REQUEST['ip']);

       //print_r($ip);
       echo $ip['cityName'];
       echo $ip['countryName'];
    }
?>

<form method="post">
    <input type="text" name="ip" />
    <input type="submit" name="submit" value="Find" />
</form>

I need to style "echo $ip['cityName']" here. I tried a lot methods, but I am only getting an error.

我需要在这里设置“echo $ip['cityName']”的样式。我尝试了很多方法,但我只得到一个错误。

回答by Jothi Kannan

You can style it by the following way:

您可以通过以下方式对其进行样式设置:

echo "<p style='color:red;'>" . $ip['cityName'] . "</p>";
echo "<p style='color:red;'>" . $ip['countryName'] . "</p>";

回答by Naji Shmly

Echo inside an HTML element with classand style the element:

在 HTML 元素中回显class并设置元素样式:

echo "<span class='name'>" . $ip['cityName'] . "</span>";

回答by Glavi?

You can "style echo" with adding new HTML code.

您可以通过添加新的 HTML 代码来“样式回显”。

echo '<span class="city">' . $ip['cityName'] . '</span>';

回答by Mohammad Masoudian

echo '<span style="Your CSS Styles">' . $ip['cityName'] . '</span>';

回答by Mohammad Masoudian

Store your results in variables, and use them in your HTML and add the necessary styling.

将结果存储在变量中,并在 HTML 中使用它们并添加必要的样式。

$usercity = $ip['cityName'];
$usercountry = $ip['countryName'];

And in the HTML, you could do:

在 HTML 中,您可以执行以下操作:

<div id="userdetails">
<p> User's IP: <?php echo $usercity; ?> </p>
<p> Country: <?php echo $usercountry; ?> </p>
</div>

Now, you can simply add the styles for countryclass in your CSS, like so:

现在,您可以简单地country在 CSS 中为class添加样式,如下所示:

#userdetails {

/* styles go here */

}

Alternatively, you could also use this in your HTML:

或者,您也可以在 HTML 中使用它:

<p style="font-size:15px; font-color: green;"><?php echo $userip; ?> </p>
<p style="font-size:15px; font-color: green;"><?php echo $usercountry; ?> </p>

Hope this helps!

希望这可以帮助!

回答by Source

You cannot style a variable such as $ip['countryName']

您不能设置诸如 $ip['countryName'] 之类的变量的样式

You can only style elements like p,div, etc, or classes and ids.

您只能为 p、div 等元素或类和 id 设置样式。

If you want to style $ip['countryName'] there are several ways.

如果你想样式 $ip['countryName'] 有几种方法。

You can echo it within an element:

您可以在元素中回显它:

echo '<p id="style">'.$ip['countryName'].'</p>';
echo '<span id="style">'.$ip['countryName'].'</span>';
echo '<div id="style">'.$ip['countryName'].'</div>';

If you want to style both the variables the same style, then set a class like:

如果要将两个变量设置为相同的样式,请设置如下类:

echo '<p class="style">'.$ip['cityName'].'</p>';
echo '<p class="style">'.$ip['countryName'].'</p>';

You could also embed the variables within your actual html rather than echoing them out within the code.

您还可以将变量嵌入到实际的 html 中,而不是在代码中回显它们。

$city = $ip['cityName'];  
$country = $ip['countryName'];
?>
<div class="style"><?php echo $city ?></div>
<div class="style"><?php echo $country?></div>