使用PHPBB3 3的函数解析PHP中的BB代码。我已经走了这么远:
<?php
include_once("../../forum/includes/functions_content.php");
$text = "[b]bold text here[/b] not bold here";
$uid = $bitfield = $options = '';
echo("parsing");
echo generate_text_for_storage($text, $uid, $bitfield, $options, true, true, true);
echo("finished");
?>不过,它呼应了parsing,但在此之后不再继续。我期望输出的内容大致如下:
<b>bold text here</b> not bold here任何帮助都非常感谢!
编辑
没有答案仍然有效。我正在寻找一个独立的 php页面,它使用PHPBB3 3的BBCode解析器将给定的BB代码字符串转换为HTML。
发布于 2014-02-26 15:09:27
生成bbcode是一个2步的过程,执行第一步(第一步)。
generate_text_for_storage用于将bbcode存储在数据库中,其存储为bbcode,因为您可以更改bbcode,而不需要对旧消息进行重新分析。
你要找的另一个功能是
generate_text_for_display
PHPBB有一个wiki,列出了如下内容
是你要找的那几页。
或者,您可以直接使用bbcode类,这类代码也可以工作。
$bbcode = new bbcode(base64_encode($bbcode_bitfield));
$bbcode->bbcode_second_pass($post_text, $posts_row['bbcode_uid'], $posts_row['bbcode_bitfield']);
$post_text = smiley_text($post_text);
$post_text = censor_text($post_text);您将需要
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);为最后一个工作
两个函数方法的完整代码,并有输出
<?php
ini_set('display_errors', 1);
define('IN_PHPBB', true);
$phpbb_root_path = './forum/';
$phpEx = "php";
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('viewforum');
$text = "[b]bold text here[/b] not bold here";
$uid = $bitfield = $options = '';
echo("parsing");
echo generate_text_for_storage($text, $uid, $bitfield, $options, true, true, true);
var_dump($text);
$text = generate_text_for_display($text, $uid, $bitfield, OPTION_FLAG_BBCODE );
var_dump($text);
echo("finished");哪种输出
parsing
string '[b:1vhn6cjx]bold text here[/b:1vhn6cjx] not bold here' (length=53)
array (size=1)
0 => int 1
string '<span style="font-weight: bold">bold text here</span> not bold here' (length=67)
finishedbb代码转换是一个2步的过程,让用户和海报都可以灵活地定制文章的视图。您需要先用第一个函数处理文本,然后再处理第二次才能得到html。
发布于 2014-02-24 15:58:11
自己做这件事的一种方法可能是使用正则表达式来查找BBCode标记并捕获两个标记之间的内容。
下面是一个粗体文本的示例:
$text = "[b]bold text here[/b] not bold here but still [b]bold here[/b]";
$pattern = '/\[b\](.*?)\[\/b\]/i';
$replacement = '<b>$1</b>';
echo preg_replace($pattern, $replacement, $text);输出:<b>bold text here</b> not bold here but still <b>bold here</b>。
更多关于替换的信息。
您可以注意到*?令牌使捕获变得懒惰而不贪婪,从而在同一个字符串中处理多个标记。
这个regexp对斜体文本,下划线文本也会起作用(稍加修改后)。但是你必须为链接,列表或图片写一个不同的。你可以在维基百科:BB码标签上找到BB代码标签的列表。在同一页上,您将找到每种标记的HTML代码示例,这将真正帮助您!
现在,有了PHP BBCode解析库。这将节省大量时间,而且可能比使用regexp更具有性能。
下面是两个库示例:PECL和梨。
发布于 2014-02-24 18:36:01
这是我根据你发布的代码得到的一个工作版本..。
1)将PHPBB3安装在我的本地web服务器上.这是: XAMPP,PHP5.3.18在windows上。
2)通过创建论坛并以“来宾”的身份发布消息,检查了所有这些内容。
好吧,到目前为止.
然后,我编辑了我的“index.php”文件,使其包含所有标准的“PHPBB3”内容,但删除了所有显示代码。
然后,我包含了您的代码并检查了每一步。
<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
echo '<br />----------------------------------------------<br />';
echo "Hello from PHPBB3! Start<br />"; // show starting
// note that multibyte support is enabled here
$sampleText = "[b]bold text here[/b] not bold here";
var_dump($sampleText . ' : '. __FILE__ . __LINE__);
$myNormalizeText = utf8_normalize_nfc($sampleText);
var_dump($myNormalizeText .' : '. __FILE__. __LINE__);
// variables to hold the parameters for submit_post
$uid = $bitfield = $options = '';
echo("<br />parsing Start<br/>");
generate_text_for_storage($myNormalizeText, $uid, $bitfield, $options, true, true, true);
var_dump($myNormalizeText .' :'. __FILE__. __LINE__);
var_dump($uid .' :'. __FILE__. __LINE__);
echo("<br />Parsing finished<br/>");
echo "<br />Goodbye from PHPBB3! END";
echo '<br />----------------------------------------------<br />';
?>输出:
----------------------------------------------
Hello from PHPBB3! Start
string '[b]bold text here[/b] not bold here : P:\developer\xampp\htdocs\phpBB3\index.php25' (length=82)
string '[b]bold text here[/b] not bold here : P:\developer\xampp\htdocs\phpBB3\index.php33' (length=82)
parsing Start
string '[b:vkw79dbw]bold text here[/b:vkw79dbw] not bold here :P:\developer\xampp\htdocs\phpBB3\index.php41' (length=99)
string 'vkw79dbw :P:\developer\xampp\htdocs\phpBB3\index.php42' (length=54)
Parsing finished
Goodbye from PHPBB3! END
------------------------这似乎是按要求做的。
https://stackoverflow.com/questions/21939777
复制相似问题