WhyCan Forum(哇酷论坛)

人过留名,雁过留声,感谢各位朋友不离不弃地支持。 QQ: 516333132, 微信: whycan_cn (哇酷网/挖坑网/填坑网) admin@whycan.cn

您尚未登录。

#1 2020-02-14 10:55:35

phpweb
会员
注册时间: 2020-02-05
累计积分: 46

php7 处理 bbcode

平台 ubuntu 18.04

1. 安装依赖包:
sudo apt-get install php php-dev php-pear -y




2. 克隆bbcode c 代码:
git clone https://github.com/esminis/php_pecl_bbcode



3. 编译:
cd php_pecl_bbcode
phpize --clean
phpize
./configure
make
make install
参考: https://pear.php.net/manual/en/pyrus.commands.build.php






4. 修改 php.ini 配置文件:
/etc/php/7.4/cli/php.ini   (cgi)
/etc/php/7.4/apache2/php.ini (apache2)

添加:
extension=bbcode





5. 测试:

<?php
$arrayBBCode=array(
    ''=>         array('type'=>BBCODE_TYPE_ROOT,  'childs'=>'!i'),
    'i'=>        array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<i>',
                    'close_tag'=>'</i>', 'childs'=>'b'),
    'url'=>      array('type'=>BBCODE_TYPE_OPTARG,
                    'open_tag'=>'<a href="{PARAM}">', 'close_tag'=>'</a>',
                    'default_arg'=>'{CONTENT}',
                    'childs'=>'b,i'),
    'img'=>      array('type'=>BBCODE_TYPE_NOARG,
                    'open_tag'=>'<img src="', 'close_tag'=>'" />',
                    'childs'=>''),
    'b'=>        array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<b>',
                    'close_tag'=>'</b>'),
);
$text=<<<EOF
[b]Bold Text[/b]
[i]Italic Text[/i]
[url]http://www.php.net/[/url]
[url=http://pecl.php.net/][b]Content Text[/b][/url]
[img]http://static.php.net/www.php.net/images/php.gif[/img]
[url=http://www.php.net/]
[img]http://static.php.net/www.php.net/images/php.gif[/img]
[/url]
EOF;
$BBHandler=bbcode_create($arrayBBCode);
echo bbcode_parse($BBHandler,$text);

输出:

<b>Bold Text</b>
[i]Italic Text[/i]
<a href="http://www.php.net/">http://www.php.net/</a>
<a href="http://pecl.php.net/"><b>Content Text</b></a>
<img src="http://static.php.net/www.php.net/images/php.gif" />
<a href="http://www.php.net/">
[img]http://static.php.net/www.php.net/images/php.gif[/img]
</a>

以上可以用 php命令行测试, 也可以用apache2网页服务器测试.

参考: http://www.zhaiqianfeng.com/2016/08/php-pecl-bbcode.html

离线

#2 2020-02-14 14:30:01

phpweb
会员
注册时间: 2020-02-05
累计积分: 46

Re: php7 处理 bbcode

试了一下, 上面这种c代码的解析器,功能太弱鸡了, 果断pass

离线

#3 2020-02-14 16:21:30

phpweb
会员
注册时间: 2020-02-05
累计积分: 46

Re: php7 处理 bbcode

找到一个不错的php bbcode解析库,

源码地址: https://github.com/s9e/TextFormatter/

演示地址: http://s9e.github.io/TextFormatter/demo.html

composer地址: https://packagist.org/packages/s9e/text-formatter

安装方法: composer require s9e/text-formatter

官方文档: https://s9etextformatter.readthedocs.io/

演示代码:

require __DIR__ . '/../vendor/autoload.php';

use s9e\TextFormatter\Bundles\Forum as TextFormatter;

$text = 'To-do list:
[list=1]
  [*] Say hello to the world :)
  [*] Go to http://example.com
  [*] Try to trip the parser with [b]mis[i]nes[/b]ted[u] tags[/i][/u]
  [*] Watch this video: [media]http://www.youtube.com/watch?v=QH2-TGUlwu4[/media]
[/list]

[code]
#include <stdio.h>
int main()
{
}
[/code]


[quote]
asdfasdfasdfasdfasdfa
asdfasdf
asdf
asdfasdf
a3453
[/quote]
';

// Parse the original text
$xml = TextFormatter::parse($text);

// Here you should save $xml to your database
// $db->query('INSERT INTO ...');

// Render and output the HTML result
echo TextFormatter::render($xml);

// You can "unparse" the XML to get the original text back
assert(TextFormatter::unparse($xml) === $text);

php test.php 运行结果:

To-do list:
<ol style="list-style-type:decimal">
  <li> Say hello to the world <img alt=":)" class="emoji" draggable="false" src="https://twemoji.maxcdn.com/2/svg/1f642.svg"></li>
  <li> Go to <a href="http://example.com">http://example.com</a></li>
  <li> Try to trip the parser with <b>mis<i>nes</i></b><i>ted<u> tags</u></i></li>
  <li> Watch this video: <span data-s9e-mediaembed="youtube" style="display:inline-block;width:100%;max-width:640px"><span style="display:block;overflow:hidden;position:relative;padding-bottom:56.25%"><iframe allowfullscreen="" scrolling="no" style="background:url(https://i.ytimg.com/vi/QH2-TGUlwu4/hqdefault.jpg) 50% 50% / cover;border:0;height:100%;left:0;position:absolute;width:100%" src="https://www.youtube.com/embed/QH2-TGUlwu4"></iframe></span></span></li>
</ol>

<pre><code>#include &lt;stdio.h&gt;
int main()
{
}
</code></pre><script async="" crossorigin="anonymous" data-hljs-style="github-gist" integrity="sha384-zmUXofHyFIwgOUqZ7LgJySh3+QxRXTN5r9PV86t5Wu1m8yixc2x3UyDkFTmKH5L1" src="https://cdn.jsdelivr.net/gh/s9e/hljs-loader@1.0.6/loader.min.js"></script>


<blockquote class="uncited"><div>
asdfasdfasdfasdfasdfa<br>
asdfasdf<br>
asdf<br>
asdfasdf<br>
a3453
</div></blockquote>

离线

#4 2020-02-14 17:20:30

phpweb
会员
注册时间: 2020-02-05
累计积分: 46

离线

页脚