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 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊