Forum Moderators: coopster

Message Too Old, No Replies

BBcode problem, i dont get it

         

rrreee2

2:24 am on Aug 18, 2010 (gmt 0)

10+ Year Member



the idea is that i will be able to parse 3 diffrent type of strings(urls), depending on how dumb the user is,
these strings
hxxp://www.youtube.com/watch?v=7gZ0QVtULHI
hxxp://www.youtube.com/watch?v=7gZ0QVtULHI&feature=related
7gZ0QVtULHI

here is my code
$getpost=$_POST['post'];
$getpost=preg_replace("'\[youtube\].*?=(.*?)&.*?\[/youtube\]'is",'yt link is \\1',$getpost);
$getpost=preg_replace("'\[youtube\].*?=(.*?)\[/youtube\]'is",'yt link is \\1',$getpost);
$getpost=preg_replace("'\[youtube\](.*?)\[/youtube\]'is",'yt link is \\1',$getpost);



if i parse this text once it comes out okay
[youtube]http://www.youtube.com/watch?v=7gZ0QVtULHI&feature=related[/youtube]
[youtube]http://www.youtube.com/watch?v=7gZ0QVtULHI[/youtube]
[youtube]7gZ0QVtULHI[/youtube]

the results look like this:
yt link is 7gZ0QVtULHI
yt link is 7gZ0QVtULHI
yt link is 7gZ0QVtULHI

(like they should^^)

if i do this twice like this
[youtube]http://www.youtube.com/watch?v=7gZ0QVtULHI&feature=related[/youtube]
[youtube]http://www.youtube.com/watch?v=7gZ0QVtULHI[/youtube]
[youtube]7gZ0QVtULHI[/youtube]
[youtube]http://www.youtube.com/watch?v=7gZ0QVtULHI&feature=related[/youtube]
[youtube]http://www.youtube.com/watch?v=7gZ0QVtULHI[/youtube]
[youtube]7gZ0QVtULHI[/youtube]

the results come out like this:
yt link is 7gZ0QVtULHI
yt link is 7gZ0QVtULHI[/youtube]
yt link is 7gZ0QVtULHI
yt link is hxxp://www.youtube.com/watch?v=7gZ0QVtULHI
[youtube]7gZ0QVtULHI

(all fuzzy)

why does that happend?

coopster

8:41 pm on Dec 15, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Because you are running it twice ;)

The PHP function parse_url() may come in handy as opposed to a regular expression. And parse_str [php.net] too. Otherwise, you merely need to adjust your regular expression and watch for greediness. I would also convert the .* to a negative class search. For example, you want everything up until the equals sign, so search for one of more of anything that is NOT an equals sign followed by an equal sign, as opposed to the .* that is.