如何从文本中删除特定用户的所有bbcode引用块?
我期待删除PHP中的BBCode作出引号,像这样的例子:如何从文本中删除特定用户的所有bbcode引用块?
[quote=testuser]
[quote=anotheruser]a sdasdsa dfv rdfgrgre gzdf vrdg[/quote]
sdfsd fdsf dsf sdf[/quote]
the rest of the post text
我期待在做一个阻塞系统,所以用户不必看那些他们不”内容不想要。所以说“testuser”被封锁了,他们不希望整个引用部分,包括嵌套在里面的第二个引用,因为这是主引用的一部分。
所以职位将只剩下:
后文
我想知道的最佳方式的其余部分做这个。我希望正则表达式,但它更复杂,我想,我有这样的尝试:
/\[quote\=testuser\](.*)\[\/quote\]/is
然而,然后捕获所有最终报价标记。
是否有另一种方法是快速的,或者是我的正则表达式的一个很好的解决方法?
总结:删除被阻止用户的初始报价以及该报价中的所有内容,但除此之外没有其他内容。
据我所知,这不是一个简单的过程。这里是我的步骤...
- 使用
preg_split()
来划分输入字符串3种方式:打开报价标签,关闭报价标签和其他。我正在拆分开始和结束标记,但使用DELIM_CAPTURE
将它们保留在输出数组中并保留原始位置/顺序。使用NO_EMPTY
以便在foreach循环中没有无用的迭代。 - 循环遍历生成的数组并搜索用户名称以省略。
- 当发现由所述目标用户报价,存储元件的起始索引,并设置
$open
为1 - 每当一个新的开引号标记发现
$open
递增。 - 每当发现新的结束标记标记
$open
递减。 - 只要
$open
达到0
,$start
和end
索引被送到range()
生成一个数组填充两点之间的数字。 -
array_flip()
,当然,将值移到键。 -
array_diff_key()
删除由preg_split()
生成的数组中的点的范围。 - 如果一切顺利,
implode()
会将子串重新粘合在一起,只保留所需的组件。
代码:(Demo)
/*
This function DOES NOT validate the $bbcode string to contain a balanced number of opening & closing tags.
This funcion DOES check that there are enough closing tags to conclude a targeted opening tag.
*/
function omit_user_quotes($bbcode,$user){
$substrings=preg_split('~(\[/?quote[^]]*\])~',$bbcode,NULL,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
$opens=0; // necessary declaration to avoid Notice when no quote tags in $bbcode string
foreach($substrings as $index=>$substring){
if(!isset($start) && $substring=="[quote={$user}]"){ // found targeted user's first opening quote
$start=$index; // disqualify the first if statement and start searching for end tag
$opens=1; // $opens counts how many end tags are required to conclude quote block
}elseif(isset($start)){
if(strpos($substring,'[quote=')!==false){ // if a nested opening quote tag is found
++$opens; // increment and continue looking for closing quote tags
}elseif(strpos($substring,'[/quote]')!==false){ // if a closing quote tag is found
--$opens; // decrement and check for quote tag conclusion or error
if(!$opens){ // if $opens is zero ($opens can never be less than zero)
$substrings=array_diff_key($substrings,array_flip(range($start,$index))); // slice away unwanted elements from input array
unset($start); // re-qualify the first if statement to allow the process to repeat
}
}
}
}
if($opens){ // if $opens is positive
return 'Error due to opening/closing tag imbalance (too few end tags)';
}else{
return trim(implode($substrings)); // trims the whitespaces on either side of $bbcode string as feature
}
}
/* Single unwanted quote with nested innocent quote: */
/*$bbcode='[quote=testuser]
[quote=anotheruser]a sdasdsa dfv rdfgrgre gzdf vrdg[/quote]
sdfsd fdsf dsf sdf[/quote]
the rest of the test'; */
/* output: the rest of the test */
/* Complex battery of unwanted, wanted, and nested quotes: */
$bbcode='[quote=mickmackusa]Keep this[/quote]
[quote=testuser]Don\'t keep this because
[quote=mickmackusa]said don\'t do it[/quote]
... like that\'s a good reason
[quote=NaughtySquid] It\'s tricky business, no?[/quote]
[quote=nester][quote=nesty][quote=nested][/quote][/quote][/quote]
[/quote]
Let\'s remove a second set of quotes
[quote=testuser]Another quote block[/quote]
[quote=mickmackusa]Let\'s do a third quote inside of my quote...
[quote=testuser]Another quote block[/quote]
[/quote]
This should be good, but
What if [quote=testuser]quotes himself [quote=testuser] inside of his own[/quote] quote[/quote]?';
/* output: [quote=mickmackusa]Keep this[/quote]
Let's remove a second set of quotes
[quote=mickmackusa]Let's do a third quote inside of my quote...
[/quote]
This should be good, but
What if ? */
/* No quotes: */
//$bbcode='This has no bbcode quote tags in it.';
/* output: This has no bbcode quote tags in it. */
/* Too few end quote tags by innocent user:
(No flag is raised because the targeted user has not quoted any text) */
//$bbcode='This [quote=mickmackusa] has not end tag.';
/* output: This [quote=mickmackusa] has not end tag. */
/* Too few end quote tags by unwanted user: */
//$bbcode='This [quote=testuser] has not end tag.';
/* output: Error due to opening/closing tag imbalance (too few end tags) */
/* Too many end quote tags by unwanted user:
(No flag is raised because the function does not validate the bbcode text as fully balanced) */
//$bbcode='This [quote=testuser] has too many end[/quote] tags.[/quote]';
/* output: This tags.[/quote] */
$user='testuser';
echo omit_user_quotes($bbcode,$user); // omit a single user's quote blocks
/* Or if you want to omit quote blocks from multiple users, you can use a loop:
$users=['mickmackusa','NaughtySquid'];
foreach($users as $user){
$bbcode=omit_user_quotes($bbcode,$user);
}
echo $bbcode;
*/
在添加解释之前,我需要做一些更多的测试。 – mickmackusa
所有帮助表示赞赏:) – NaughtySquid
@NaughtySquid现在我对它很有信心。 – mickmackusa
你想要的是一个正则表达式不可能的。我会建议扫描文件,直到找到[quote=testuser]
。找到它后,设置一个布尔值以开始过滤,并将计数器设置为1.在布尔值为true后,为每个[quote=...]
标记递增计数器。每遇到一个[/quote]
标签,减少计数器。当计数器达到0时,将用于过滤的布尔值更改为false。
这是一些sudocode。您可能需要根据您的应用程序对其进行修改,但我认为它显示了使用的一般算法。
filtering = false
counter = 0
for each line:
if line contains "[quote=testuser]"
filtering = true
counter = 0
if line contains "[quote="
counter += 1
if line contains "[/quote]
counter -= 1
if counter = 0
filtering = false
if not filtering
print line
这听起来像希望正确的轨道,是否有一个例子在某种地方这样的技术? – NaughtySquid
我会尽量写一个简单的例子或看看我能不能找到一个 – digitaLink
你是明星谢谢你! – NaughtySquid
既然有能力巢报价,你是不是要能够让你独自一人要与正则表达式的确切功能。没有办法跟踪你已经通过了多少[quote = ...]。 – digitaLink
试试这个'(\ [quote =)(。*?)]((\ s | \ S)*?(\ [\/quote \]))*'因为只有第一个引号和最后一个引号是几乎所有的事情。如果您使用替代,组2('$ 2')等于第一个引号用户名。 https://regex101.com/r/0pkmgl/2 – GrumpyCrouton
你能写出你想要隐藏的东西吗?或写出你想看到的出来 –