Tom
Tom

Reputation: 199

Trouble with PHP preg_replace()

I feel like an amateur for asking this, but I've been struggling at this for a long time and can't solve the problem.

I'm making a forum with embedded YouTube videos and a rich text editor. I need a function to convert the HTML tag to BBCode, another to convert the BBCode back to HTML.

BBCode to HTML

$message = (get message from database);
$A = '/\[youtube](.*?)\[\/youtube\]/is';
$B = '<object type="application/x-shockwave-flash" data="http://www.youtube.com/v/$1" width="425" height="350"><param name="movie" value="http://www.youtube.com/v/$1"><param name="wmode" value="transparent"></object>';
preg_replace($A, $B, $message);

I need a script that reverses this process, turning the HTML into BBCode. If someone could help me out I would be extremely grateful, or maybe suggest a easier method if one exists, or even a link that could provide some insight into the issue.

Thank you.

Upvotes: 1

Views: 92

Answers (1)

Popnoodles
Popnoodles

Reputation: 28409

Try this

$message = 'Some text
<object type="application/x-shockwave-flash" data="http://www.youtube.com/v/abcdefgh" width="425" height="350"><param name="movie" value="http://www.youtube.com/v/abcdefgh"><param name="wmode" value="transparent"></object>
More text
<object type="application/x-shockwave-flash" data="http://www.youtube.com/v/abcdefgh" width="425" height="350"><param name="movie" value="http://www.youtube.com/v/abcdefgh"><param name="wmode" value="transparent"></object>
Even more text';

echo preg_replace("/\<object.*?youtube\.com\/v\/(.*?)\".*?object\>/ims", "[youtube]$1[/youtube]", $message);

Upvotes: 1

Related Questions