English 中文(简体)
在一个论坛上援引
原标题:Recursive quote in a forum

我在与个人论坛共同撰写的网站上为我个人论坛撰写了短文功能。

引出的标语见[引用=用户名]message[/quote],因此我写道:

$str=preg_replace( #[quote=(.*?)](.*?)[/quote]#is ,  <div class="messageQuoted"><i><a href="index.php?explore=userview&userv=$1">$1</a> wrote :</i>$2</div> , $str);

如果报价是一例,但用户则引用该笔意向书。 因此,我需要一种“recursive quote来适用这一行为。

I tried to searching on SO many topics, but I don t really understand how it can works. Would be appreciated any suggestions/tips for do this kind of operation! Let me know, and thanks!

EDIT

At the end, this is my own solution :

if(preg_match_all( #[quote=(.*?)](.*?)#is , $str, $matches)==preg_match_all( #[/quote]#is , $str, $matches)) {
    array_push($format_search,  #[quote=(.*?)](.*?)#is );
    array_push($format_search,  #[/quote]#is );

    array_push($format_replace,  <div class="messageQuoted"><a class="lblackb" href="index.php?explore=userview&userv=$1">$1</a> wrote :<br />$2 );
    array_push($format_replace,  </div> );
}

$str=preg_replace($format_search, $format_replace, $str);

it repleace only if the number of occurences is correct. So it should (right?) to prevent html broke or other malicious attack. What do you think?

最佳回答

You can simply replace the opening quote tag with the opening div tag and same for the closing section. This only goes bad if the user messes up it s quote tag matching. Alternatively you can recurse the quote function with the inner section:

<?php
function quote($str)
{
    if( preg_match( #[quote=.*?](.*)[/quote]#i , $str) )
         return quote(preg_replace( #[quote=.*?](.*)[/quote]#i ,  $1 , $str);
    return preg_replace( #[quote=.*?](.*)[/quote]# ,  <div blabla>$1</div> , $str);
}
?>
问题回答

PCRE and regexes in PHP do allow for recursion http://php.net/manual/en/regexp.reference.recursive.php - You will need the (?R) syntax for that.

But it usually only matches recursively, it does not apply your replacement string recursively. Hencewhy you need to use preg_replace_callback at the very least.

It s difficult to get working, but I believe (totally untested) this might do in your case:

= preg_replace_callback( #[quote=(.*?)]((?:(?R)|.*?)+)[/quote]#is ,
           cb_bbcode_quote , $str);

现在该回话回馈了封面的内容,因为它不得不再次对口号(match)中文本援引同样的reg,并预示着_replace——回话。

Recursive syntax like this is precisely when regular expressions start being too weak. You should look into using some kind of parser instead.

Regular expressions (at least without some extensions), can only accept regular languages. In order to have a recursive syntax, you need a context-free language. These require more sophisticated parsers.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签