preg_replace删除自定义标记之间的换行符


preg_replace remove linebreak between custom tags

因此,如果用户输入[tag]hello[/tag],那么效果会很好。但是,如果用户输入:

[tag]你好

你好吗

表单中有换行符,[tag][/tag]之间的内容不会受到操作的影响。(.*?)这里少了什么东西吗?

function tag($string) {
    $rules = array(
        '#'[tag](.*?)'['/tag']#i' => '<font style="color: #7c7c7c; font-style: italic;"> $1 <br><hr style="border-style: dotted; border-top: none; border-color: #9f9f9f;"></font>'
    );
    foreach ($rules as $link => $player)
        $string = preg_replace($link, $player, $string);
    return $string;
}

你可以试试这个对我有用的方法:

<?php
//
function tag($string) {
  $rules = array(
    '#'[tag](.*?)'['/tag']#is' => '<font style="color: #7c7c7c; font-style: italic;"> $1 <br><hr style="border-style: dotted; border-top: none; border-color: #9f9f9f;"></font>'
  );
  foreach ($rules as $link => $player)
  {
    $string = preg_replace($link, $player, $string);
  }
  return $string;
}
//
// test 1:
//
$string = '[tag]Hello How are you[/tag]';
//
echo "<textarea style='"width: 700px; height: 90px;'">"
  . tag($string)
  . "</textarea>";
//
// test 2:
//
$string = '[tag]Hello
How are you[/tag]
';
//
echo "<textarea style='"width: 700px; height: 90px;'">"
  . tag($string)
  . "</textarea>";
?>

修饰符"is"是:

i: ignore case.
s: treat the text in single line mode.