Jump to content

MySQL-query based on BBCode


Lemontree

Recommended Posts

Hi!

 

Can anyone please help me form script for doing a MySQL query based on variables in a bbcode?

 

BBCode could be like this: [datatype::dataid]

 

Explode datatype and dataid intro separate strings and using them in querys.

 

Example of string containing bbcodes:

$body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fringilla, enim eget dictum sodales, enim velit iaculis dolor, sed congue orci diam id ipsum. Integer ullamcorper elit ac arcu condimentum id elementum arcu tempus. Ut aliquam commodo eros, mollis tristique magna adipiscing eu.
[video::3456]
Duis congue turpis quis eros porta eu sollicitudin mi porttitor. Ut ac nisi id risus imperdiet aliquet. Suspendisse lacinia sem id ipsum blandit egestas. Mauris id vulputate ligula. In in quam urna. Nam purus augue, laoreet vel sodales nec, rutrum non augue. 
[video::6789]
Phasellus facilisis commodo arcu, vel dapibus tortor pretium eu. Integer elit nisi, condimentum ut vehicula eget, ullamcorper non quam. Nullam suscipit tempor consequat. Vestibulum sollicitudin ante tristique neque vestibulum a fermentum nulla venenatis. In eget nulla nulla.";

 

I found something close here:

http://stackoverflow.com/questions/2801228/php-bbcode-with-sql-selection

Link to comment
Share on other sites

Hi again.

 

I found an answer to it myself. Worked a little with the code in the example on the link in previous post.

 

I post the script here, in case someone need it:

$body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fringilla, enim eget dictum sodales, enim velit iaculis dolor, sed congue orci diam id ipsum. Integer ullamcorper elit ac arcu condimentum id elementum arcu tempus. Ut aliquam commodo eros, mollis tristique magna adipiscing eu.<br /><br />[video:1]<br /><br />Duis congue turpis quis eros porta eu sollicitudin mi porttitor. Ut ac nisi id risus imperdiet aliquet. Suspendisse lacinia sem id ipsum blandit egestas. Mauris id vulputate ligula. In in quam urna. Nam purus augue, laoreet vel sodales nec, rutrum non augue.<br /><br />[video:2]<br /><br />Phasellus facilisis commodo arcu, vel dapibus tortor pretium eu. Integer elit nisi, condimentum ut vehicula eget, ullamcorper non quam. Nullam suscipit tempor consequat. Vestibulum sollicitudin ante tristique neque vestibulum a fermentum nulla venenatis. In eget nulla nulla.";

preg_match_all ( '#\[video:(.*?)\]#i', $body, $matches, PREG_SET_ORDER );

for ( $i = 0, $j = count( $matches ); $i < $j; $i++ )
{
echo $matches[$i][0].", with ID: ";
echo $matches[$i][1]."<br />";

$result = mysql_query("SELECT * FROM video WHERE id='".$matches[$i][1]."' AND validated='1' LIMIT 1");
while($row = mysql_fetch_array($result)) {
	$vid = $row['id'];
	$videoid = $row['videoid'];
	$videotype = $row['videotype'];
}

switch ($videotype):
    case 1:
    	$body = str_replace($matches[$i][0], '<iframe width="650" height="366" src="http://www.youtube.com/embed/'.$videoid.'?autohide=1&border=0&modestbranding=1&showinfo=1&iv_load_policy=3&rel=0" frameborder="0" allowfullscreen></iframe>', $body);
        break;
    case 2:
    	$body = str_replace($matches[$i][0], '<iframe src="http://player.vimeo.com/video/'.$videoid.'?byline=0&portrait=0&color=910007" width="650" height="366" frameborder="0"></iframe>', $body);
        break;
    default:
        echo "";
endswitch;
}

echo "<br />".$body;

 

If it can be optimized, please do so... ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.