Home > Backend Development > PHP Tutorial > 这样的正规表达式怎么写?

这样的正规表达式怎么写?

WBOY
Release: 2016-06-23 14:25:09
Original
1042 people have browsed it

Mon-Fri 8:00-23:00; Sat 10:00-23:00; Sun 10:00-22:00;

把上面变成 周一~周五8:00-23:00; 周六10:00-23:00; 周日10:00-22:00;

$str="Mon-Fri 8:00-23:00; Sat 10:00-23:00; Sun 10:00-22:00;"
$en=array("Mon", "Fri","Sat","Sun");
$cn=array("周一", "周五","周六","周日");
$str=str_replace($en, $cn, $str);

请问如何把英文之间的"-"变成~, 时间中间的不替换?
英文和数字间的空格如何替换?


回复讨论(解决方案)

$en=array("Mon-Fri","Sat","Sun");
$cn=array("周一~周五","周六","周日");
$str=str_replace($en, $cn, $str);

可以在你的代码的基础上加增加
$str = preg_replace('/-/','~',$str,1);

$str="Mon-Fri 8:00-23:00; Sat 10:00-23:00; Sun 10:00-22:00"; $en=array("Mon", "Fri","Sat","Sun"); $cn=array("周一", "周五","周六","周日"); $str=str_replace($en, $cn, $str); //echo $str; $a[0]='/([^\d]+)\-/'; $a[1]='/\s(\d)/'; $b[0]='\1~'; $b[1]='\1'; $str=preg_replace($a,$b, $str); echo $str;
Copy after login

就是楼上这样!

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