匹配URL中年份并替换指定字符串

需求:

https://img1.artron.net/auction/2018/art513829/d/art5138291001.jpg  
替换成
https://img1.artron.net/auction/poly/art513829/d/art5138291001.jpg

解决方案:

一、
$a = "https://img1.artron.net/auction/2018/art513829/d/art5138291001.jpg";
$b = substr_replace($a,"poly",-17).substr($a,-13);

二、
$a = "https://img1.artron.net/auction/2018/art513829/d/art5138291001.jpg";
$b = preg_replace('/\d{4}/','poly',$a);

 

你可能感兴趣的:(学习笔记,PHP)