简体   繁体   English

如何在网页中将 php 代码和 html 标签显示为文本

[英]How to display php code and html tags as text in a web page

I have the following piece of code and wish to display it as text in a webpage.我有以下一段代码,并希望将其显示为网页中的文本。 How can I do it?我该怎么做? I wat the code mentioned below to be displayed as text in a webpage.我看下面提到的代码在网页中显示为文本。 The page might be either a .html page or a .php page.该页面可能是 .html 页面或 .php 页面。 Kindly help.请帮忙。

<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>Log in!</title>
 <style type="text/css">
  div {
     width:300px;
     height:120px;
     margin: 0px auto;
     margin-top: 100px;
      padding: 30px 20px;
      border:2px solid #00F;
     background-color:#0CF;
     border-radius:10px;
    }

 span{
    margin:0px auto;
     color:#F00;
      font-family:Verdana, Geneva, sans-serif;
     font-size:12px;
  }

 </style>
 </head>

<body>
 </body>
 <?php
 require_once('conn.php');

$sql="select * from contacts where contacts_id=14";
 $result=$mysqli->query($sql);
 $row=$result->fetch_array();
 if( isset($_POST['id']) && $row['email']==$_POST['id'] && $_POST['password']==$row['password'])
 {
     header("location:view.php")    ;
      $_SESSION['sid']= $row['email'];
    $flag=1;
 }
 ?>

 <div>
 <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
 <table align="center" border="0">
   <tr>
      <td>Id</td>
      <td>:</td>
      <td><input type="text" name="id" placeholder="Email" /></td>
    </tr>
    <tr>
    <td>Password</td>
      <td>&:</td>
      <td><input type="password" name="password" /></td>
    </tr>
    <tr>
      <td></td>
   <td colspan="2"><input type="submit" value="Log In!" /></td>
    </tr>
 </table>
  </form>
     <?php
      if (isset($_POST['id']) && isset($_POST['password']) && $flag==0)
     {
           echo "<center><span>Invalid Id/Password</span></center>";    
      }

   ?>
    </div>
   </body>
   </html>

You can also use highlight_string() function您还可以使用highlight_string()函数

 <?php

 highlight_string("YOUR STRING/CODE GOES HERE");
 ?>
$gen_query_data= htmlspecialchars(' $'.$db_val_clean.'_ser = $_POST["'.$sel_name_clean.'_ser"];')."<br>";

     echo $gen_query_data;  

please change your variables请更改您的变量

if your server isn't set up to process .html for the php tag, you're fine to do exactly what you've done.如果您的服务器没有设置为处理 php 标记的 .html,那么您可以完全按照您所做的去做。

You can also use this: &lt;?php which would render as <?php but not be seen by PHP.你也可以使用这个: &lt;?php它将呈现为<?php但不被 PHP 看到。

One option you can use is PHP's highlight_file() function:您可以使用的一种选择是 PHP 的highlight_file()函数:

<?php
highlight_file("your_file.php");
?>

You can also use .html file extensions highlight_file("your_file.html") .您还可以使用.html文件扩展名highlight_file("your_file.html")

Even a .txt file extension highlight_file("your_file.txt") .即使是.txt文件扩展名highlight_file("your_file.txt")

Consult:咨询:

this question is pretty old , but as long as no one used this solution so this would be helpful .这个问题已经很老了,但只要没有人使用这个解决方案,这就会有所帮助。

using htmlspecialchars :使用htmlspecialchars

htmlspecialchars — Convert special characters to HTML entities htmlspecialchars — 将特殊字符转换为 HTML 实体

echo htmlspecialchars("your special character goes here");

暂无
暂无

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

相关问题 如何使用 PHP 解析 HTML 并将其中的所有标签显示为纯文本除了<br>标签 - How to parse HTML using PHP and display as plain text all tags within <code></code> except <br> tag 有没有替代方案<xml> ,<pre> , or escaping HTML tags to display HTML tags on a web page for a typing game program?</pre></xml> - Is there any alternative for <xml>, <pre>, <code> or escaping HTML tags to display HTML tags on a web page for a typing game program? 如何使用 ajax 中的 get 方法仅将 html 代码“(不包括标签)”的文本部分传递给 php 页面 - How to pass only text part of html code `(excluding the tags)` to php page using get method from ajax 如何在 html 页面中动态显示 php 代码 - How to display php code in html page dynamically 如何在页面上显示 PHP 和 HTML 源代码? - How to display PHP & HTML source code on a page? 在网页上显示PHP代码 - Display PHP code on web page 如何在页面上将html / javascript代码显示为普通文本 - How to display html/javascript code as normal text on page 如何从服务器获取带有HTML标记的文本并通过HTML标记在客户端显示它? (使用php和angularjs) - How get text with HTML tags from server and display it in client side with HTML markup? (using php and angularjs) 如何将响应文本显示为html并同时发送到php页面 - how to display response text as html and at the same time send to a php page PHP / HTML:在同一网页上显示结果 - PHP/HTML : Display result on the same web page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM