解析错误:语法错误,PHP 中出现意外的“{”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6685621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:59:35  来源:igfitidea点击:

Parse error: syntax error, unexpected '{' in PHP?

phpsyntax-error

提问by Alper

So I got this error:

所以我得到了这个错误:

Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\scanner\mine.php on line 112

解析错误:语法错误,C:\xampp\htdocs\scanner\mine.php 中第 112 行出现意外的“{”

On this line:

在这一行:

if(preg_match("inurl:", $text) {

Its part of a "Clean" function:

它是“清洁”功能的一部分:

  function Clean($text) {
        if(preg_match("inurl:", $text) {
            str_replace("inurl:", "", $text);
            return htmlspecialchars($text, ENT_QUOTES);
        } else {
            return htmlspecialchars($text, ENT_QUOTES);
        }
  }

How can I fix it?

我该如何解决?

回答by

Two problems, One closing ) and a missing semicolon after str_replace, also you should know your str_replace won't do anything in your code so i added $text = ... :)

两个问题,一个结束)和 str_replace 后缺少分号,你也应该知道你的 str_replace 不会在你的代码中做任何事情,所以我添加了 $text = ... :)

  function Clean($text) {
        if(preg_match("inurl:", $text)) {
            $text = str_replace("inurl:", "", $text);
            return htmlspecialchars($text, ENT_QUOTES);
        } else {
            return htmlspecialchars($text, ENT_QUOTES);
        }
  }

回答by Ribose

Add another ):

添加另一个)

if(preg_match("inurl:", $text)) {

回答by prodigitalson

You're missing the closing )on your if statement.

你错过了)if 语句的结尾。

Should be:

应该:

if(preg_match("inurl:", $text)) {

if(preg_match("inurl:", $text)) {

You're also missing your statement terminator on the string replace. That should be:

您还缺少字符串替换中的语句终止符。那应该是:

str_replace("inurl:", "", $text);

回答by Quentin

You left out the )for the ifcondition.

你离开了)if状态。

  if
    (
      preg_match(
        "inurl:", $text
      ) 
    {

回答by mermshaus

The if clause is superfluous anyway. The following function is equivalent to the original version (sans the non-compiling regex pattern).

无论如何,if 子句是多余的。以下函数等效于原始版本(没有非编译正则表达式模式)。

function Clean($text)
{
    $text = str_replace("inurl:", "", $text);
    return htmlspecialchars($text, ENT_QUOTES);
}

If the string you want to replace is not found in the haystack, str_replace won't do anything. So it is safe to run the function unconditionally.

如果在 haystack 中找不到要替换的字符串,则 str_replace 不会执行任何操作。所以无条件运行该函数是安全的。

In the original version, you'd also have to surround the regex pattern for preg_match with delimiters (see: http://www.php.net/manual/en/regexp.reference.delimiters.php). (In this case, strpos would do the job, too.)

在原始版本中,您还必须用分隔符包围 preg_match 的正则表达式模式(请参阅:http: //www.php.net/manual/en/regexp.reference.delimiters.php)。(在这种情况下, strpos 也可以完成这项工作。)