Php 使用str_replace或其他函数替换标记

Php 使用str_replace或其他函数替换标记,php,Php,是否有方法仅在同时存在打开和关闭时替换标记,例如: //search to replace $text = '[b]this will be bold[/b] but this will be not.'; $search = array('[b]long string[/b]'); //replace with $replace = array('<b>long string</b>'); echo str_replace($search, $replace, $tex

是否有方法仅在同时存在打开和关闭时替换标记,例如:

//search to replace
$text = '[b]this will be bold[/b] but this will be not.';
$search = array('[b]long string[/b]');
//replace with
$replace = array('<b>long string</b>');
echo str_replace($search, $replace, $text);
//搜索以替换
$text='[b]这将是粗体[/b],但这将不是。”;
$search=array(“[b]长字符串[/b]”);
//取代
$replace=数组(“长字符串”);
echo str_replace($search,$replace,$text);
通缉结果:

这将是大胆的,但这将不是


我不知道如何正确设置,任何帮助都将不胜感激。

您可以使用正则表达式来完成此操作:

$text = '[b]this will be bold[/b] but this will be not.';
$text = preg_replace('/\[([a-z]+)\](.*?)\[\/\1\]/', '<\1>\2</\1>', $text);
$text='[b]这将是粗体的[/b],但这不是。”;
$text=preg\u replace('/\[([a-z]+)\](.*?\[\/\1\]/','\2',$text);

像这样的东西就可以了:

//search to replace
$text = '[b]this will be bold[/b] but this will be not.';
$search = array('~\[([^]]+)\]([^][]*)\[/\1\]~');
//replace with
$replace = array('<$1>$2</$1>');
echo preg_replace($search, $replace, $text);

买主须知:这是非常简化的并且忽略了一些严重的安全问题。实际上,您必须考虑到一些事情,如不允许
,以及其他可能导致跨站点脚本注入的标记

与这类事情一样,解析器很可能胜过正则表达式

如果您确实想使用正则表达式:
([^]]+)
替换为一组列在白名单上的标记。例如:

\[(b|em|i|strong)\]([^][]*)\[/\1\]  // allows only b, em, i, and strong

听起来您想要实现一个BBCodes系统,这需要使用正则表达式来实现

有一篇非常好的文章解释了如何做到这一点,并解释了各种regex部分的含义,以便您以后可以编写自己的补充内容

但是,要单独转换上述示例的代码如下所示:

$text = '[b]this will be bold[/b] but this will be not.';
$ret = $text; // So we don't overwrite the original variable.
$ret = preg_replace('#\[b\](.+)\[\/b\]#iUs', '<b>$1</b>', $ret);
$text='[b]这将是粗体的[/b],但这不是。”;
$ret=$text;//所以我们不会覆盖原始变量。
$ret=preg\u replace(“\[b\]”(.+)\[\/b\]\\iUs',“$1',$ret);

我为您编写了高级BBcode解析函数

您可以在
$tags='b | url'中添加任何BBcode

比如说

$tags = 'b|url|i|e|img';
它还支持带有内部标记的bbcode,例如
[url=http://www.website.com]blaa[/url]

这是完整的代码

function parseBBCODE($text)
{
    //bbcodes tags
    $tags = 'b|url';


    //loop tags sub tags too
    while (preg_match_all('#\[('.$tags.')=?(.*?)\](.+?)\[/\1\]#is',$text, $matches))
    foreach ($matches[0] as $key => $match)
    {
       //extract tag info
       list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]);

        //match tags and replace them
        switch ($tag)
        {
          //Bold
          case 'b':
             $replacement    = '<b>'.$innertext.'</b>';
          break;
          //link url
          case 'url':
            $replacement = '<a target="_blank" href="'.($param ? $param : $innertext).'">'.$matches[3][$key].'</a>';
          break;
          default :
           $replacement = "";
        }

      $text = str_replace($match, $replacement,$text);
      unset($match,$replacement,$param);
    }

   return $text;
}


 $search = '[b]long string [/b]  [url]http://www.google.com[/url]   [url=http://www.google.com]url with tag[/url]';

 echo parseBBCODE($search);
函数parseBBCODE($text)
{
//bbcodes标签
$tags='b | url';
//循环标记和子标记
while(preg#u match#u all('.\[('.$tags.')=?(.*?)\](.+?)\[/\1\].\35; is',$text,$matches))
foreach($matches[0]作为$key=>$match)
{
//提取标签信息
list($tag、$param、$innertext)=数组($matches[1][$key]、$matches[2][$key]、$matches[3][$key]);
//匹配标签并替换它们
交换机($tag)
{
//大胆的
案例“b”:
$replacement='.$innertext';
打破
//链接url
案例“url”:
$replacement='';
打破
违约:
$replacement=“”;
}
$text=str_replace($match,$replacement,$text);
未设置($match,$replacement,$param);
}
返回$text;
}
$search='[b]长字符串[/b][url]http://www.google.com[/url][url=http://www.google.com]带有标记[/url]'的url;
回显密码($search);

您需要替换模式而不是特定字符串。正则表达式允许这样做。为什么不允许(一些)HTML标记呢?或者想想markdown(有很多库支持它)+1支持markdown。Michel Fortin在这里有一个非常好的Markdown解析器:这可能非常不安全,但是,根据XSSAgreed,您将实现比规则更多的异常:/@是的,我同意;这就是为什么我在底部有一个关于它的注释。@TwoWholeForms No argument。这非常有效,谢谢,要将标记限制为我想要的一次,需要为我想要的每个标记创建一个正则表达式吗?例如,如果我只允许斜体和粗体,并且我需要更多的标记..这非常适合在数组上实现我自己的正则表达式,谢谢:)
$tags = 'b|url|i|e|img';
function parseBBCODE($text)
{
    //bbcodes tags
    $tags = 'b|url';


    //loop tags sub tags too
    while (preg_match_all('#\[('.$tags.')=?(.*?)\](.+?)\[/\1\]#is',$text, $matches))
    foreach ($matches[0] as $key => $match)
    {
       //extract tag info
       list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]);

        //match tags and replace them
        switch ($tag)
        {
          //Bold
          case 'b':
             $replacement    = '<b>'.$innertext.'</b>';
          break;
          //link url
          case 'url':
            $replacement = '<a target="_blank" href="'.($param ? $param : $innertext).'">'.$matches[3][$key].'</a>';
          break;
          default :
           $replacement = "";
        }

      $text = str_replace($match, $replacement,$text);
      unset($match,$replacement,$param);
    }

   return $text;
}


 $search = '[b]long string [/b]  [url]http://www.google.com[/url]   [url=http://www.google.com]url with tag[/url]';

 echo parseBBCODE($search);