doudiewen9435
2013-03-14 00:11 阅读 26
已采纳

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongmaonao0505 dongmaonao0505 2013-03-14 00:16

    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);
    
    点赞 评论 复制链接分享

相关推荐