作者:83984246_42dbe6 | 来源:互联网 | 2018-03-22 20:15
这个类可以删除或保存在一个字符串中的重复文字。
并且可以删除第几个位置上的重复文字
1.处理类
class StringClean{
public function keep_nth($string, $subject, $replacement='|CLEANUP|', $nth=1){
$cOntents= $this->replace_nth($string, $subject,$replacement, $nth);
$cOntents= str_replace($replacement,$string,$contents);
return $contents;
}
public function replace_nth($string, $subject, $replacement='', $nth=1){
$regExPattern = "/".preg_quote($string, '/')."/";
return preg_replace_callback($regExPattern,
function($found) use (&$regExPattern, &$replacement, &$nth) {
$nth--;
if ($nth==0) return preg_replace($regExPattern, $replacement, reset($found) );
return reset($found);
}, $subject,$nth );
}
}
2.使用方法
//呵呵以网站域名为例(顺便打个小广告)
$cOntents='https://www.php1.cn';
$StringRemoveDuplicates = new StringClean;
$cOntents= $StringRemoveDuplicates->keep_nth('codepearl', $contents,"", 2);
var_dump($contents );
//输出:https://www.php1.cn