Home  >  Article  >  Backend Development  >  正则可否跳字?该如何处理

正则可否跳字?该如何处理

WBOY
WBOYOriginal
2016-06-13 13:46:29766browse

正则可否跳字?
正则能否完成下面两个替换?
1.   200320042006   替换为   (2003)(2004)(2006)
说明:把200?替换为(200?)其中?的字符不变

2.;;;;;;;;替换为;;;-;;;;
说明:匹配指定N次数的符号,前N-1不变,第N个替换


如果正则不能,有否其他较简单替代办法?

------解决方案--------------------
//1
$a=200320042006;
echo preg_replace( '/(\d{4})/ ', '(\1) ',$a);
//2
$a= ';;;;;;;; ';
$n=4;
echo preg_replace( '/(;{ '.($n-1). '});/ ', '\1- ',$a);

?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn