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

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

WBOY
Release: 2016-06-13 10:05:08
Original
815 people have browsed it

正则可否跳字?
正则能否完成下面两个替换?
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);

?>

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template