PHP BBCode相关的问题。如何获得两个标签之间的值? [英] PHP BBCode related issue. How to get values between two tags?
问题描述
$ comment =[item] Infinity Edge [/ item]我的网站需要做以下工作: < br> [item]蛋酒健康药水[/ item];
$ this-> site-> bbcode-> postBBCode($ comment);
BBCode函数如下所示:
函数postBBCode($ string)
{
$ string = nl2br($ string);
$ string = strip_tags($ string,'< br>< / br>');
$ string = $ this-> tagItem($ string);
返回$ string;
}
函数tagItem($ string)
{
//获取[item]和[/ item]值之间的所有值
// Appoint他们到一个数组。
//在数组中foreach item_name,调用convertItems($ item_name)函数。
//现在,数组中的每个item_name将被替换为任何convertItems($ item_name)函数返回的值。
//返回修改字符串
}
函数convertItems($ itemName)
{
// - 我已经创建了这个函数,但是让我解释它的作用。
//用$ itemName查询数据库。
//从数据库获取item_image。
//返回'< img src =$ row ['item_image']>< / img>';
}
好的,我已经在功能之间问过我的问题了,希望你能理解我想要做的事情。
基本上,[item]和[/ item]标签之间的任何东西都会被转换成图像,但是每个项目的图像路径将从数据库中获取。
我有困难的时候正确地获取[item]和[/ item]标签之间的值,它应该获得它找到的所有正确匹配,而不是第一个匹配。 >解决方案
如果您在$ string上使用preg_match_all,您将得到一个包含所有匹配项的结果集:
$ results = array();
preg_match_all('#\ [item\](。*?)\ [\ / item\]#',$ string,$结果);
$ results将有一个如下所示的结果数组:
数组
(
[0] =>数组
(
[0] => [item] Infinity Edge [/ item]
[1] => [item]蛋黄生理药水[/ item]
)
[1] =>阵列
(
[0] =>无限边缘
[1] => Eggnog Health Potion
)
)
现在您应该可以遍历$ results [1]并通过convertItems发送它。
I need to do the following for my website.
$comment = "[item]Infinity Edge[/item]<br>[item]Eggnog Health Potion[/item]";
$this->site->bbcode->postBBCode($comment);
The BBCode function is like this:
function postBBCode($string)
{
$string = nl2br($string);
$string = strip_tags($string, '<br></br>');
$string = $this->tagItem($string);
return $string;
}
function tagItem($string)
{
//Get all values between [item] and [/item] values
//Appoint them to an array.
//foreach item_name in array, call convertItems($item_name) function.
//Now, each item_name in array will be replaced with whatever convertItems($item_name) function returns.
//return modified string
}
function convertItems($itemName)
{
// -- I already made this function, but let me explain what it does.
//Query the database with $itemName.
//Get item_image from database.
//Return '<img src="$row['item_image']></img>';
}
Okay, I already asked my questions between functions. I hope you understood what I am trying to do.
Basically, anything between [item] and [/item] tags will be converted into an image, but the image path of each items will be taken from database.
The part I am having hard times is getting values between [item] and [/item] tags correctly. It should be getting all the correct matches it finds, not the first match.
If you use a preg_match_all on $string, you'll get a result set with all the matches:
$results = array();
preg_match_all('#\[item\](.*?)\[\/item\]#', $string, $results);
$results will have an array of results that looks like this:
Array
(
[0] => Array
(
[0] => [item]Infinity Edge[/item]
[1] => [item]Eggnog Health Potion[/item]
)
[1] => Array
(
[0] => Infinity Edge
[1] => Eggnog Health Potion
)
)
Now you should be able to loop through the $results[1] and send it through convertItems.
这篇关于PHP BBCode相关的问题。如何获得两个标签之间的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!