Author Topic: BB Code  (Read 661 times)

Offline CygnusX

  • Level 24
  • *
  • Posts: 303
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
BB Code
« on: January 17, 2011, 10:04:47 AM »
I'm planning on using the code found below for BB code for my site:

http://forums.codecharge.com/posts.php?post_id=77123

I have been able to modify it so far to my liking, however, I desire to add YouTube embedded videos.... and my regular expression knowledge is not very good.  I have considered adding:

'/\[YT\](.*?)\[\/YT\]/is',

and

<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/$1?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$1?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>

... but this would require people to copy out only the ?v variable from the URL.  I had also considered changing '/\[YT\](.*?) to '/\[YT\]http://www.youtube.com/watch?v\=(.*?)   but, on occasion, youtube urls contain an & after the ?v, and this would need to be cut out (and I'm not sure how to do this).

Any ideas?  I tried googling for existing, but php bb code pulls up to many phpbb code results.  Also, reg-ex seems to have several poor tutorials on the web.  : \
« Last Edit: January 17, 2011, 10:08:49 AM by CygnusX »

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: BB Code
« Reply #1 on: January 17, 2011, 10:20:24 AM »
Simple, cheat :D At least I would :)

Don't use regex, just allow all words (entity that is the first elment of a string or has space before it AND ends with a space or there is no other character in the string after it) that start with: "http://www.youtube.com, http://youtube.com, www.youtube.com, youtube.com".

Offline CygnusX

  • Level 24
  • *
  • Posts: 303
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
Re: BB Code
« Reply #2 on: January 17, 2011, 10:25:14 AM »
The intent isn't to link to the youtube video, but rather to embed it.  Using BB code with regex is the only good solution I can think of for this.  Otherwise, I'd have to store lengthy embed code (which neither myself or the user would like) and it would still be difficult to sort through since all post have to get sent through html_entities()

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: BB Code
« Reply #3 on: January 17, 2011, 11:13:16 AM »
Make [ movie ]http://www.youtube.com/v/$1?fs=1&amp;hl=en_US[ /movie ], the user provide only the link and you generate embedded code.

Offline DJK

  • Level 2
  • *
  • Posts: 5
  • Reputation: +0/-0
    • View Profile
Re: BB Code
« Reply #4 on: February 12, 2011, 02:33:35 PM »
I know this is a semi-old topic but maybe something like this could work.

Please forgive the structure, trying to do it notepad really does suck..
Code: [Select]
function bbcodeParse($text) {
//Here you have an array with basics (example - of course you'd have more)
$bbcode = array (
     '#\[b\](.*?)\[/b\]#si' => '<span style="font-weight: bold;">\\1</span>'
);

//Now convert YouTube links

$text = preg_replace_callback('#\[yt\](.*?)\[\/yt\]#is', create_function('$ytUrl',
'
if (filter_var($ytUrl[2], FILTER_VALIDATE_URL)) {
$ret = \'<object width="480" height="385"><param name="movie" value="\'.$ytUrl[2].\'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$1?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>\';
return $ret;
}
'
), $text);

//The continue to parse the rest of the bbcodes
$text = preg_replace ( array_keys ($bbcode), array_values ($bbcode), $text);
return nl2br($text);
}
Usage: [yt]http://www.youtube.com/watch?v=HXx1S6O-T30[/yt]
Untested but I don't see any reason it shouldn't work.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal