繁体   English   中英

preg_replace方法的PHP语法错误

[英]PHP syntax error on preg_replace method

我正在尝试做一个可以创建个性化标签的bbcode解析器类,但是url有一些问题

由于使用了正则表达式,我已经做了所有我想做的事,而没有特别的问题,但是当我尝试创建一个指向指定URL的特殊标记时遇到了问题。

在我的课堂上,我添加了如下方法:

<?
    private function check_url_from_bbcode ($tag = "url", $css = null, $url_path = null) {
        $regex_url = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'";
        if (!isset ($css)) $css = $this->css_link;
        $regex_url = $this->regex_url;
        if (isset ($url_path)) $url_path = "$url_path/";
        $this->bbcode = preg_replace ("/\[$tag\]([$regex_url]*)\[\/$tag\]/", "<a title=\"Vai al link\" class=\"$css\" href=\"$url_path$1\">$1</a>", $this->bbcode);
        $this->bbcode = preg_replace ("(\[$tag\=([$regex_url]*)\](.+?)\[/$tag\])", "<a title=\"Vai al link\" class=\"$css\" href=\"$url_path$1\">$2</a>", $this->bbcode);
    }
?>

该代码可与经典网址[url] http://ciao.com [/ url]和[url = http://ciao.com] ciao [/ url]一起正常使用

但是在使用特殊的URL主题页作为last.fm样式的情况下,我遇到了一些问题,因此[artist] Lemon Jelly [/ artist]。

bblink在<a href =“ http://ciao.com/artist/Lemon Jelly”> Lemon Jelly </ a>中转换 (我用空格<a>仅显示链接代码)。
该链接在href属性上具有空格,因此永远无法使用。

<?
    private function check_url_from_bbcode ($tag = "url", $css = null, $url_path = null) {
        $regex_url = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'";
        if (!isset ($css)) $css = $this->css_link;
        $regex_url = $this->regex_url;
        if (isset ($url_path)) $url_path = "$url_path/";
        // begin of the problem
        $this->bbcode = preg_replace ("/\[$tag\]([$regex_url]*)\[\/$tag\]/", "<a title=\"Vai al link\" class=\"$css\" href=\"$url_path".str_replace (" ", "+", $1)."\">$1</a>", $this->bbcode);
        // end of the problem
        $this->bbcode = preg_replace ("(\[$tag\=([$regex_url]*)\](.+?)\[/$tag\])", "<a title=\"Vai al link\" class=\"$css\" href=\"$url_path$1\">$2</a>", $this->bbcode);
    }
?>

为避免这种情况,我写了一些代码,用相同的URL更改了href url部分,但用“ +”代替了“”空格字符,因此[artist] Lemon Jelly [/ artist]应该变成< href =“ http://ciao.com/artist/Lemon+Jelly”>柠檬果冻</ a>

我没有使用PHP的经验,也不知道是什么问题,我在其他情况下使用了这种语法而没有遇到问题。

有人可以帮我找到我错了吗?

错误类型为PHP Parse错误:语法错误,意外的T_LNUMBER,/中应为T_VARIABLE或'$'

请在出现错误的行之前和之后提供2-3行(行号应在PHP分析错误中)。

这听起来不像是正则表达式问题,更像是逃避问题。

我对SO很陌生,为什么我不能对这个问题发表评论(因为我写的并不是一个答案)

解析错误是由于$1不是有效的PHP变量名称。 它们必须以字母或下划线开头。 preg_replace参数中填充的$1变量仅在参数内部有效。

尝试更多类似这样的方法:

$this->bbcode = preg_replace("/\[$tag\]([$regex_url]*)\[\/$tag\]/e", "'<a title=\"Vai al link\" class=\"$css\" href=\"$url_path'. str_replace(' ', '+', '$1'). '\">$1</a>'", $this->bbcode);

e修饰符将替换字符串评估为PHP代码,因此应生成所需的字符串。

请注意,我目前无法对其进行测试,因此您可能需要对其进行一些调整。 抱歉:]
编辑没关系,我拥有一个FTP客户端。 修复了正则表达式。 有用 :)

为避免这种情况,我编写了一些代码,将href网址部分更改为相同的网址,但用“ +”代替了“”

为什么不在标签内容上使用urlencode

请注意,urlencode仅应用于查询参数。 实际的目录组件应使用rawurlencode ,因为HTTP本身不使用+而不是空格。

我相信问题在于$ 1引用,该引用在preg_replace参数之外的str_replace函数中使用。 $ 1反向引用仅在preg_replace参数的范围内起作用,因此不能像变量一样将其传递给str_replace 它尝试使用$ 1作为变量,但是在PHP中对于变量名无效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM