我使用这段代码,一切都很好,它隐藏文本从$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);
发布于 2013-02-26 19:52:10
你只需要改变
$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-26 19:55:22
您必须为每一对创建模式:
$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;
https://stackoverflow.com/questions/15087797
复制相似问题