【问题标题】:How to analyze HTML source code in textarea如何在 textarea 中分析 HTML 源代码
【发布时间】:2021-05-17 16:39:57
【问题描述】:

您好,我会解释我的问题。我正在创建一个站点,多个用户通过登录可以插入页面的源代码,特别是浏览器游戏的源代码(我曾想过通过 textarea 这样做)。接下来我要分析这个源代码,并通过在保留区域中形成一个表格从中获取一些数据。

我有几个问题要问,用户粘贴到textarea的源代码是否需要保存在数据库中?我像长文本一样保存它,但是当我尝试打印它时,我没有得到实际代码,而是一系列 /n / n / n / n / n / n / n / n / n /。

我怎样才能分析这段代码并获得我需要的数据?非常感谢你,我希望我已经很好地解释了自己。

例如,这是我在 caserma_admin.php 中的 textarea 代码:

  <form action = "add_caserma.php" method = "post">
  <textarea name="caserma" class="textarea" rows="30" cols="50"></textarea><br>
  <button type="submit" class="submit_textarea" name= "invia"> Invia </button>
  </form>

接下来在 add_caserma.php 中,我在数据库中插入用户的 html 源代码:

    $caserma = $_POST["caserma"];
    $username = $_SESSION["utente"];
    
    
    $username = mysqli_real_escape_string($connessione , $username); //protezione stringhe
    $caserma = mysqli_real_escape_string($connessione , $caserma); //protezione stringhe
    
    $query = "UPDATE utenti SET caserma = '$caserma' WHERE username = '$username'";

    $trova_utente = mysqli_query($connessione, $query);
  if(!$trova_utente){
    die('RICHIESTA FALLITA!' . mysqli_error($connessione));
  }else{
    $_SESSION["caserma"] = $caserma;
  }
}

在 test.php 中我尝试标记它:

<?php include 'database.php' ?>
<?php session_start(); ?>

<?php
echo $_SESSION["caserma"];
 ?>

当我打印此源代码时,代码本身会被 n / n / n / n / n / 替换,但这不是问题,因为我对打印不感兴趣,只是出于好奇。我感兴趣的是如何从插入到 textarea 中的源代码中获取数据。 在我的数据库中,源代码是这样的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="mainLayout">
    <head>
        <title>Anglosphere x3</title>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="imagetoolbar" content="no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="content-language" content="en-US" />
<meta name="viewport" content="width=device-width">
...
...

当我打印时:

r\n\r\n\r\n \r\n \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n \r\n \r\n \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n \r\n
\r\n
\r\n
\r\n \r\n
\r\n \r\n \r\n \r\n\r\n
\r\n 
\r\n
\r\n \r\n \r\n\r\n \r\n \r\n \r\n

我想澄清一下,我对打印代码不感兴趣,而是对从表格中获取数据感兴趣

【问题讨论】:

  • 我建议您使用第三方库中的一些编辑器,例如 ckeditor 来处理文本的编码和解码。由于文本必须以其原始原始格式存储和处理,您需要检查网络是否支持数据,是的,您可以使用长文本或 blob 将其存储在数据库中,只需在编辑器中定义最大字符限制也。
  • 请提供一些示例代码。
  • 完成,我已经更改了原问题的描述
  • 您需要发布仅显示更新查询的输出字符串的部分。
  • 完成,非常感谢

标签: php html mysql dom textarea


【解决方案1】:

您可以为此使用simplexml

$html = <<<HTML
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="mainLayout">
    <head>
        <title>Anglosphere x3</title>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="imagetoolbar" content="no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="content-language" content="en-US" />
<meta name="viewport" content="width=device-width" />
</head>
</html>
HTML;

var_dump(simplexml_load_string($html));

工作example

【讨论】:

  • 据我了解,SimpleXML 将 XML 文档转换为数据结构,您可以将其作为数组和对象的集合进行迭代。为此,我必须使用以下语法: $ html =
  • 它只是一个字符串。 &lt;&lt;&lt;BLA 只是一个符号来封装一个名为 heredoc 的字符串 - 所以是的,你可以把任何字符串放在那里,不管它来自哪里。
  • 好的,我可以这样写吗? $CasermaUtente = $_SESSION["caserma"]; $html =
  • 技术上是的,但你可以写var_dump(simplexml_load_string($_SESSION["caserma"]));
  • 它向我显示了这 3 个警告:警告: simplexml_load_string():实体:第 1 行:解析器错误:需要开始标记,在 C:\- 中找不到“警告: simplexml_load_string(): \r\n" -//W3C//DTD XHTML 1.0 Transitional//EN\" \"ww in C:\------------------------.php 上线10 警告: simplexml_load_string(): ^ in C:\------------------------------------.php 第 10 行 bool(false)
猜你喜欢
  • 1970-01-01
  • 2019-09-22
  • 2011-04-17
  • 2015-01-22
  • 2010-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多