1

我使用这段代码,一切正常,它隐藏了$comments包含之间的文本[],但我想隐藏其他符号的文本。前任。** && ^^ $$ ## // <>. 我需要在这里添加什么,而不是

Date <20.02.2013> Time [11-00] Name #John#

拥有这个:

Date Time Name 

?

function replaceTags($startPoint, $endPoint, $newText, $source) {
    return preg_replace('#('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')#si', '$1'.$newText.'$3', $source);
}

$source= $comments;
$startPoint='[';
$endPoint=']';
$newText='';
echo replaceTags($startPoint, $endPoint, $newText, $source);
4

2 回答 2

1

你只需要改变

$startPoint='[';
$endPoint=']';

$startPoint='<';
$endPoint='>';

要执行多个符号,您可以对函数进行多次调用,如下所示:

$source= $comments;
$newText='';

$str = replaceTags('[', ']', $newText, $source);
$str = replaceTags('<', '>', $newText, $str);
$str = replaceTags('*', '*', $newText, $str);
$str = replaceTags('&', '&', $newText, $str);
$str = replaceTags('^', '^', $newText, $str);
$str = replaceTags('$', '$', $newText, $str);
$str = preg_replace("/\#[^#]+#)/","",$str);
$str = replaceTags('/', '/', $newText, $str);

// add more here
echo $str;
于 2013-02-26T11:52:09.750 回答
0

您必须为每对创建模式:

$pairs = array(
  '*' => '*',
  '&' => '&',
  '^' => '^',
  '$' => '$',
  '#' => '#',
  '/' => '/',
  '[' => ']', // this
  '<' => '>', // and this pairs differently from the rest
);

$patterns = array();

foreach ($pairs as $start => $end) {
  $start = preg_quote($start, '/');
  $end = preg_quote($end, '/');

  $patterns[] = "/($start)[^$end]*($end)/";
}

echo preg_replace($patterns, '', $s), PHP_EOL;
//  not sure what you want here
//  echo preg_replace($patterns, '$1' . $newText . '$2', $s), PHP_EOL;
于 2013-02-26T11:55:22.427 回答