开发者

PHP syntax error on preg_replace method

开发者 https://www.devze.com 2022-12-13 23:48 出处:网络
I\'m trying to do a bbcode parser class that can create personalyzed tags, but I have some problem with urls

I'm trying to do a bbcode parser class that can create personalyzed tags, but I have some problem with urls

I've did all I need without particular problems thanks to regular expressions but I have a problem when I try to create a special tag who point to a specified URL.

In my class I've added a method like this:

<?
    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开发者_JAVA百科->bbcode);
    }
?>

the code works fine with classical urls [url]http://ciao.com[/url] & [url=http://ciao.com]ciao[/url]

but I have some problem with the case of a special url subject page as last.fm style, so [artist]Lemon Jelly[/artist].

The bblink is converted in < a href="http://ciao.com/artist/Lemon Jelly">Lemon Jelly< /a> (I've used the spaces < a> only to show the link code).

The link has the whitespaces on the href attribute so can't never work.

<?
    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);
    }
?>

To avoid this, I've wrote a little part of code that change the href url portion with tha same url but with "+" in place of " " whitespace char, so [artist]Lemon Jelly[/artist] should became < a href="http://ciao.com/artist/Lemon+Jelly">Lemon Jelly< /a>

I'm not experienced with PHP and I'm not sure what is the problem, I've uses this syntax in other situation without encounter the problem.

can someone help me to find where I'm wrong?

the error type is PHP Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /...


Please provide the 2-3 lines before and after the line that has the error (line number should be in the PHP parse error.

This doesn't sound like a regex problem... more like an escaping issue.

I'm rather new to SO, why couldn't i just commented to the question to ask this (as what i wrote isn't really an answer)


The parse error is due to the fact that $1 is not a valid PHP variable name. They must start with a letter or an underscore. The $1 variable populated in the preg_replace parameters are just valid inside the parameters.

Try something more like this:

$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);

The e modifier evaluates the replacement string as PHP code, so that should generate the string you want.

Note, I can't test it right now, so you may need to tweak it a bit. Sorry :]
Edit Never mind that, I got a hold of a FTP client. Fixed the regex. It works :)


To avoid this, I've wrote a little part of code that change the href url portion with tha same url but with "+" in place of " "

Why not use urlencode on the contents of the tag?

Note that urlencode should only be used for query parameters; actual directory components should use rawurlencode, as HTTP itself doesn't use + instead of spaces.


I believe the problem is with the $1 reference, which is used in the str_replace function outside of the preg_replace arguments. The $1 backreference only works within the confines of preg_replace arguments, so you can't pass it like a variable to str_replace; it tries to use $1 as a variable, but in PHP that is invalid for a variable name.

0

精彩评论

暂无评论...
验证码 换一张
取 消