Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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 从mysql insert语句中删除链接?_Php_Mysql_Regex - Fatal编程技术网

Php 从mysql insert语句中删除链接?

Php 从mysql insert语句中删除链接?,php,mysql,regex,Php,Mysql,Regex,我想知道是否有人可以帮忙,我正在运行一个MySQL insert查询,所以当用户填写表单时,它会将内容插入数据库。但是,我正在尝试这样做,以便可以删除/阻止插入的链接(URL) 我正在尝试这个,但我是MySQL新手,无法让它工作,我不确定我做得是否正确,如果有人能帮助我,我将不胜感激 提前感谢, <?php ob_start(); ?> <?php // check if the review form has been sent if(isset($_POST['revi

我想知道是否有人可以帮忙,我正在运行一个MySQL insert查询,所以当用户填写表单时,它会将内容插入数据库。但是,我正在尝试这样做,以便可以删除/阻止插入的链接(URL)

我正在尝试这个,但我是MySQL新手,无法让它工作,我不确定我做得是否正确,如果有人能帮助我,我将不胜感激

提前感谢,

<?php ob_start(); ?>
 <?php 
// check if the review form has been sent
if(isset($_POST['review_content']))
if(isset($_POST['review_recipient']))
{
    $content = $_POST['review_content'];
    $review_recipient = $_POST['review_recipient'];
        //We remove slashes depending on the configuration
        if(get_magic_quotes_gpc())
        {
                $content = stripslashes($content);
                $review_recipient = stripslashes($review_recipient);
        }
        //We check if all the fields are filled
        if($_POST['review_content']!='')
        if($_POST['review_recipient']!='')
        {


            {

                $forbidden = array('<[\w.]+@[\w.]+>', '<\w{3,6}:(?:(?://)|(?:\\\\))[^\s]+>', '#<.*?>([^>]*)</a>#i');
$matches  = array('****', '****', '****');
$post     =  preg_replace($forbidden, $matches, $post);


            $sql = "INSERT INTO ptb_reviews (id, from_user_id, from_guest, to_user_id, content) VALUES (NULL, '-1', '".$review_recipient."', '".$profile_id."', '".$content."');";
            mysql_query($sql, $connection);

            $_SESSION['message']="<div class=\"infobox-wallpost\"><strong>Thank You</strong> - Your review has been sent and is awaiting approval.</div><div class=\"infobox-close4\"></div>"; 
header("Location: {$_SERVER['HTTP_REFERER']}");

} } } } } ?>


preg replace,有一个用于查找URL的正则表达式:

$inputData = "www.google.com is a url";
$filteredData = preg_replace('/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.-]*)*\/?/','[blocked url]',$inputData);
这里出了问题:

$post     =  preg_replace($forbidden, $matches, $post);
这不会修复post变量中的所有URL

我想你想要这样的东西:

$regex = "/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.-]*)*\/?/";
$replacement = "[blocked url]";
$review_recipient = reg_replace($regex,$replacement,$_POST['review_recipient']);
$profile_id = intval($_POST['profile_id ']); //dont know how you get this
$content = reg_replace($regex,$replacement,$_POST['review_content']);
$regex2 = "/(.*)\b(word1|word2|word3)\b(.*)/";
$replacement2 = "[blocked word]";
$content = preg_replace(Array($regex, $regex2),Array($replacement, $replacement2),$_POST['review_content']);

您遇到的问题是,您在
get\u magic\u quotes\u gpc()
调用中进行了正则表达式检查,Joel的代码也将
reg\u replace
作为一个拼写错误,否则这会起作用(如果您将其置于magic quotes检查之外)

这里有一个完全更新的脚本供您尝试

<?php

ob_start();

// check if the review form has been sent
if(isset($_POST['review_content'])) {
    if(isset($_POST['review_recipient'])) {
        $content = $_POST['review_content'];
        $review_recipient = $_POST['review_recipient'];

        //We remove slashes depending on the configuration
        if(get_magic_quotes_gpc()) {
                $content = stripslashes($content);
                $review_recipient = stripslashes($review_recipient);
        }

        $regex = "/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.-]*)*\/?/";
        $replacement = "[blocked url]";
        $review_recipient = preg_replace($regex,$replacement,$_POST['review_recipient']);
        //$profile_id = intval($_POST['profile_id']); //dont know how you get this
        $content = preg_replace($regex,$replacement,$_POST['review_content']);


        //We check if all the fields are filled
        if($_POST['review_content']!='') {
            if($_POST['review_recipient']!='') {

                $sql = "INSERT INTO ptb_reviews (id, from_user_id, from_guest, to_user_id, content) VALUES (NULL, '-1', '".$review_recipient."', '".$profile_id."', '".$content."');";
                mysql_query($sql, $connection);

                $_SESSION['message']="<div class=\"infobox-wallpost\"><strong>Thank You</strong> - Your review has been sent and is awaiting approval.</div><div class=\"infobox-close4\"></div>";

                header("Location: {$_SERVER['HTTP_REFERER']}");
            }
        }

    }

}

?>
然后将您的
preg\u replace
更改为如下内容:

$regex = "/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.-]*)*\/?/";
$replacement = "[blocked url]";
$review_recipient = reg_replace($regex,$replacement,$_POST['review_recipient']);
$profile_id = intval($_POST['profile_id ']); //dont know how you get this
$content = reg_replace($regex,$replacement,$_POST['review_content']);
$regex2 = "/(.*)\b(word1|word2|word3)\b(.*)/";
$replacement2 = "[blocked word]";
$content = preg_replace(Array($regex, $regex2),Array($replacement, $replacement2),$_POST['review_content']);

查找url:
/(https?:\/\/)([\da-z\.-]+)\([a-z\.]{2,6})([\/\w\.-]*)*\/?/
查看我的答案,问题是你的代码嵌套。我已经清理了一点,问题很清楚。我尝试了这个方法,但对我无效。我放入$inputData=$content并仍然显示链接。也许我做得不对。如果修复了,正则表达式中有一个小错误,但我想你可能使用了错误的变量,所以它仍然是bI评估preg替换OK再次感谢您为我查看此代码,但是代码对我不起作用,我会在我的问题中发布我的更新代码,如果您能告诉我我是否在某个地方出错,这将非常有帮助