Php str_replace - 要替换的两个值? [英] Php str_replace - two values to replace?
本文介绍了Php str_replace - 要替换的两个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 str_replace 我想从此更改 $url:
Using str_replace I want to change $url from this:
$url = http://example.com/images/lala1.jpg
到这里
$url = http://example.com/images/lala1-0001.jpg
我的问题是我不知道如何插入-".
My problem is that I don't know how to insert the "-".
$url 正在改变,所以我真的只知道它的末尾有.jpg".
$url is changing so I really only know that it has ".jpg" at its' end.
到目前为止我的代码:
for($i=1;$i<=9;$i++) {
$array[] = str_replace('.jpg',sprintf("%04d",$i).'.jpg',$url); }
知道如何使这项工作成功吗?
Any idea how I can make this work?
推荐答案
Sprintf 允许在参数中使用常规字符:
Sprintf allows for regular characters in the arguments:
str_replace('.jpg', sprintf("-%04d.jpg", $i), $url);
这篇关于Php str_replace - 要替换的两个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文