Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP清理整个HTML页面以输入数据库_Php_Mysql_Sql - Fatal编程技术网

使用PHP清理整个HTML页面以输入数据库

使用PHP清理整个HTML页面以输入数据库,php,mysql,sql,Php,Mysql,Sql,从某种意义上说,我试图缓存页面,以便以后可以像以前一样显示它们(如果它们被更改或删除)。所以我将整个页面的HTML(craigslist广告)拉入数据库字段 我使用file\u get\u内容是为了使我所需要的内容更简单和容易。还有比这更多的东西,但这是我所做工作的基础 $page = file_get_contents('http://annapolis.craigslist.org/hea/3652436359.html'); // $page = mysql_real_escape_str

从某种意义上说,我试图缓存页面,以便以后可以像以前一样显示它们(如果它们被更改或删除)。所以我将整个页面的HTML(craigslist广告)拉入数据库字段

我使用file\u get\u内容是为了使我所需要的内容更简单和容易。还有比这更多的东西,但这是我所做工作的基础

$page = file_get_contents('http://annapolis.craigslist.org/hea/3652436359.html');
// $page = mysql_real_escape_string($page);
// $page = htmlspecialchars($page);
// $page = htmlentities($page);
mysql_query("INSERT INTO `page` (`html`) VALUES ('$page')");
我已经尝试了我能找到的所有内置PHP函数

  • mysql\u real\u escape\u字符串
  • htmlspecialchars
  • 不平等
所有这些都不会对页面进行足够的清理,以便将其输入MySQL数据库,MySQL每次都会抛出一个语法错误。有人告诉我只需对HTML进行base64编码并输入,但我希望能够在数据库中搜索HTML,这样就无法满足我的需要

我尝试了各种不同的方法,比如两个函数(一个在另一个内部),但我似乎无法让它正常工作

任何帮助都将不胜感激。

试试这个,它会有用的

$page = file_get_contents('http://annapolis.craigslist.org/hea/3652436359.html');

mysql_query("INSERT INTO `page` set `html`= '".magic($page)."'");

function magic($value)
{
    if( get_magic_quotes_gpc() )
       $value = stripslashes( $value );

    if( function_exists( "mysql_real_escape_string" ) )
       $value = mysql_real_escape_string( $value ); 
    else
        $value = addslashes( $value );

return $value;
}

为什么不将它们存储在普通文件而不是数据库中?如果您真的想在数据库中使用它们,您可以使用
json\u encode
?但是我建议使用普通文件,并且只将名称存储在db中。请阅读我想能够使用SQL查询来搜索特定关键字的HTML。这就是为什么我真的想把它放在数据库里。@Phil,虽然这是真的,但是对于这些新成员来说,被“不要使用MySQL扩展”轰炸是很痛苦的!也许,可以提供一个非常简单的教程来说明为什么它是错误的等等。作为tinyurl.com最常见的链接。您收到的确切错误是什么?尝试使用PDO或mysqli准备好的语句。我一直在db中保存html页面,没有任何问题(顺便说一句,htmlentities通常是没有问题的),这是正确的,但是如上所述,最好切换到mysqli扩展。