PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php – 用双引号括起字符串大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试处理可能包含单引号和其他特殊字符的文本.如果用单引号括起来,则不会继续.所以我试图将单引号字符串括在双引号字符串中.

我已经检查了以前的线程.

这是代码

检查结果:http://ideone.com/gWFdUb

<?PHP
function clean($String) {
    eval('$String = "'.$String.'";');
   $String = str_replace(' ',' ',$String); // replaces all spaces with hyphens.
   return preg_replace('/[^A-Za-z0-9 @\-]/','',$String); // Removes special chars.
}

$d =  clean('this was readlly n'ice 'test for@me to') ;
echo $d;

评估线有什么问题?

我正在处理用户推文,发布有两个目的.

>存储到@L_874_4@表中. (@L_874_4@i_real_escapE)没有帮助
>将每个字符串处理为文本以进行匹配和POS(词性)标记.

由于文字中的这些字符,我卡住了.所以在我开始处理之前尝试删除它.

更新:

检查一下,这里我已经在使用@L_874_4@i_real_escape_String,即使脚本在达到此目的时停止

...
mention-179
May Thanks @ShaleMarkets @01Finser @52York @AB_CutRock @AFSPG @AJSmith222 @AlbertaEnergy @AndyR_362_11845@arTin @Annemullettamg @APGQ_officiel-440929408564477952-Tue Mar 04 19:18:57 +0000 2014-19:03:572014:03:04201403Adnan Aftab Nizamani0131

mention-180
Thank you for @ShaleMarkets,to promoTing,thank you very much for an aWARD. Glad to have been able to Help you :)-440897048963850240-Tue Mar 04 17:10:22 +0000 2014-17:03:222014:03:04201403♘-₭ℜi℘-0582

mention-181
@ShaleMarkets https://t.co/aM8liykQqR-440890009273393152-Tue Mar 04 16:42:24 +0000 2014-16:03:242014:03:04201403Bre Burey018

提到181有什么不对,所以它被卡住了?这是代码

foreach ($tweets1 as $item)
    {       
        $count = $count + 1;
        $text = $item->text;
        //echo $userid.$text;
        $text_id = $item->id;
        $constant = 'mention';
        $time = $item->created_at;
        //echo $time;
        //$dt = new datetiR_362_11845@e('@' . strtotime($timE));
        $dt = \datetiR_362_11845@e::createFromFormat('D M d H:i:s e Y',$timE);
        //var_dump($dt);
        $tweet_time = $dt->format('H:m:s');
        $tweet_dtm = $dt->format('Y:m:d');
        $year =  $dt->format('Y'); 
        $month =  $dt->format('m'); 
        $user_name = $item->user->name;
//      echo $year.$month.$user_name;
        $inreplyto =  $item->in_reply_to_screen_name;
        $rt_count = $item->retweet_count;
        $follower_count = $item->user->followers_count;
        echo $constant."-".$count."<br>".$text."-".$text_id."-".$time."-".$tweet_time.$tweet_dtm.$year.$month.$user_name.$rt_count.$follower_count."<br>";
        echo "<br>";
        $con = @L_874_4@i_connect('127.0.0.1','root','root');         
        if (@L_874_4@i_connect_errno())
        {
            echo "Failed to connect to @L_874_4@: " . @L_874_4@i_connect_error();
            return;
        }
        $text = @L_874_4@i_real_escape_String($con,$text);
        $insertQuery1 = "INSERT INTO twitter_mention(`username`,`userid`,`tweet_text`,`text_id`,`time`,`month`,`year`,`date`,`user_follower_count`,`rt_count`,`constant`,`in_reply_to`) VALUES ('".$twitteruser."','".$userid."','".$text."','".$text_id."','".$tweet_time."','".$month."','".$year."','".$tweet_dtm."','".$follower_count."','".$rt_count."','".$constant."','".$inreplyto."')";

        if (!@L_874_4@i_query($con,$insertQuery1))
        {
        //  die('Error: ' . @L_874_4@i_error($con));
        //  echo "error";
        }
始终使用上下文转义

如果没有任何关于它的内容的背景,你不能一般地“清理”数据.不要尝试构建单个函数来处理所有可能的情况.只是不要.没有用.在你的函数中,你试图通过删除某些字符来“清理”字符串.您无法通过删除一组字符来清理字符串.这个想法是有缺陷的,因为你总是不得不允许使用一些在某些语法或其他语法中特殊的字符.

相反,根据将要使用的上下文处理字符串.例如:

>如果要在SQL查询中使用此字符串,则必须使用预准备语句(或mysqli_real_escape_string())来正确转义数据.
>如果要在HTML标记输出此值,则需要使用htmlspecialchars()来转义数据.
>如果要将其用作命令行参数,则需要使用escapeshellcmd()escapeshellarg().

进一步阅读:

> Security.SE — What’s the best way to sanitize user input in PHP?
> What’s the best method for sanitizing user input with PHP?
> Does eliminating dangerous characters avoid SQL-injection?

大佬总结

以上是大佬教程为你收集整理的php – 用双引号括起字符串全部内容,希望文章能够帮你解决php – 用双引号括起字符串所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。