views:

277

answers:

4

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

the code works fine with classical urls [url]http://ciao.com%5B/url%5D & [url=http://ciao.com%5Dciao%5B/url%5D

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 /...

+2  A: 

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)

Ken
I've cheked the other lines of code but it all ok, I've also replaced the problem line with the old one and it's turned ok, sorry look at the error, because I've wrote the right one some minutes ago
Vittorio Vittori
Comments are locked for members with less than 50 rep. See the FAQ link at the top of the page. There is a nice list there. - Actually doesn't make much sense, when you think about it; being able to answer, but not comment :P
Atli
A: 

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.

R. Bemrose
I like to use "+" char because it is for internal site links, for external link i use [url]http://etc.com[/url], so i need it because I'm writing my queries with + instead of spaces, thanks for the urlencode method, I'll use it for default links!
Vittorio Vittori
+1  A: 

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 :)

Atli
great job, I wouldn't be able to fix it without you! thanks
Vittorio Vittori
A: 

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.

JYelton
Yes you are right, Atli wrote a functional solution
Vittorio Vittori