Php 用双引号括住字符串
我试图处理可能包含单引号和其他特殊字符的文本。如果用单引号括起来,则不会继续。所以我试图把单引号字符串括起来,变成双引号字符串 我已经检查了以前的线程 代码如下: 检查结果: 提-181有什么错,所以卡住了?这是密码Php 用双引号括住字符串,php,string,Php,String,我试图处理可能包含单引号和其他特殊字符的文本。如果用单引号括起来,则不会继续。所以我试图把单引号字符串括起来,变成双引号字符串 我已经检查了以前的线程 代码如下: 检查结果: 提-181有什么错,所以卡住了?这是密码 foreach ($tweets1 as $item) { $count = $count + 1; $text = $item->text; //echo $userid.$text;
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 DateTime('@' . strtotime($time));
$dt = \DateTime::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 = mysqli_connect('127.0.0.1', 'root', 'root', 'root');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return;
}
$text = mysqli_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 (!mysqli_query($con,$insertQuery1))
{
// die('Error: ' . mysqli_error($con));
// echo "error";
}
foreach($tweets1作为$item)
{
$count=$count+1;
$text=$item->text;
//echo$userid.$text;
$text\u id=$item->id;
$constant='提及';
$time=$item->created\u at;
//回声时间;
//$dt=新日期时间('@'.strotime($time));
$dt=\DateTime::createFromFormat('dmdh:i:sey',$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\u name=$item->user->name;
//echo$year.$month.$user\u name;
$inreplyto=$item->in\u reply\u to\u screen\u name;
$rt\u count=$item->retweet\u count;
$follower\u count=$item->user->followers\u count;
echo$constant.-“$count.$text.-“$text\u id.-“$time.-”$tweet\u time.$tweet\u dtm.$year.$month.$user\u name.$rt\u count.$follower\u count.“
”;
回声“
”;
$con=mysqli_connect('127.0.0.1','root','root','root');
if(mysqli\u connect\u errno())
{
echo“未能连接到MySQL:”.mysqli_connect_error();
回来
}
$text=mysqli\u real\u escape\u字符串($con,$text);
$insertQuery1=“INSERT INTO twitter”提及(`username`、`userid`、`tweet`text`、`text`id`、`time`、`month`、`year`、`date`、`user`follower`count`、`rt`count`、`in`u reply`u to`)值(`$twitter`、`userid`、`text`、`text`、`text`、`id`、`text`、`text`、`id`、`text`、`text`、`year`、`tweet`、`time`、`tweet`、$month`、`dtu`、`count、`“.$constant.”、“'.$inreplyto.”);
if(!mysqli_query($con,$insertQuery1))
{
//die('Error:'.mysqli_Error($con));
//回声“错误”;
}
在这个答案中,我将尝试回答您最初的问题:
评估线怎么了
无。倒数第二行是唯一包含语法错误的行。您没有正确转义单引号。请尝试以下操作:
$d = clean('this was readlly n\'ice \'test for@me to');
它现在应该产生以下输出:
这是一个非常好的测试for@me到
我不确定这是否是预期的结果。如果您将问题更新为包含您正试图实现的目标,以及为什么您关心字符串中的引号类型,也许我可以帮助您找到解决方案。在这个答案中,我将尝试解决您最初的问题: 评估线怎么了 无。倒数第二行是唯一包含语法错误的行。您没有正确转义单引号。请尝试以下操作:
$d = clean('this was readlly n\'ice \'test for@me to');
它现在应该产生以下输出:
这是一个非常好的测试for@me到
我不确定这是否是预期的结果。如果您将问题更新为包含您正试图实现的目标,以及为什么您关心字符串使用哪种类型的引号,也许我可以帮助您找到解决方案。试试这个-
<?php
function clean($string) {
eval("\$string = \"$string\";");
$string = str_replace(' ', ' ', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $string); // Removes special chars.
}
$d = clean("this was readlly n'ice 'test for@me to") ;
echo $d;
?>
试试这个-
<?php
function clean($string) {
eval("\$string = \"$string\";");
$string = str_replace(' ', ' ', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $string); // Removes special chars.
}
$d = clean("this was readlly n'ice 'test for@me to") ;
echo $d;
?>
始终使用上下文转义
在没有任何上下文的情况下,您不能一般地“清理”数据。不要试图构建一个函数来处理所有可能的情况。不要这样做。这是毫无意义的。在您的函数中,您试图“清理”通过删除某些字符来清除字符串。您无法通过删除一组字符来清除字符串。这种想法是有缺陷的,因为您总是必须允许使用某些语法或其他语法中特殊的字符
相反,请根据要使用的上下文来处理字符串。例如:
- 如果要在SQL查询中使用此字符串,则必须使用预处理语句(or)来正确转义数据
- 如果要在HTML标记中输出此值,则需要使用转义数据
- 如果要将其用作命令行参数,则需要使用或
进一步阅读:
始终使用上下文转义
在没有任何上下文的情况下,您不能一般地“清理”数据。不要试图构建一个函数来处理所有可能的情况。不要这样做。这是毫无意义的。在您的函数中,您试图“清理”通过删除某些字符来清除字符串。您无法通过删除一组字符来清除字符串。这种想法是有缺陷的,因为您总是必须允许使用某些语法或其他语法中特殊的字符
相反,请根据要使用的上下文来处理字符串。例如:
- 如果要在SQL查询中使用此字符串,则必须使用预处理语句(or)来正确转义数据
- 如果要在HTML标记中输出此值,则需要使用转义数据
- 如果要将其用作命令行参数,则需要使用或
进一步阅读:
为什么要使用eval?注意str_replace(“”,,$string);
看起来很奇怪为什么要使用eval?注意str_replace(“”,,$string)看起来奇怪的@阿迈勒:但用户输入字符串,他们不` `考虑它作为用户。tweet@user123:我不知道你想说什么。你有一条推特。现在呢?你想用它做什么?你想如何修改推特?你想达到的最终结果是什么