str_replace。替换字符串中的数字
大家好,我这里有这段代码,
$pathinfo = pathinfo($fullpath);
$tags = $shortpath;
$tags = str_replace("/", " ", $tags);
$tags = str_replace("__", " ", $tags);
$tags = str_replace(".png", "", $tags);
$tags = str_replace(".jpg", "", $tags);
$tags = str_replace(".jpeg", "", $tags);
$tags = str_replace(".gif", "", $tags);
一切都与上面的工作正常,但我还需要它来替换文件开头的一些数字,我添加
的文件示例是
247991 - my_small_house.jpg
它是“-”之前的数字我需要离开 这可以做到吗?
谢谢
HI all I have this code here
$pathinfo = pathinfo($fullpath);
$tags = $shortpath;
$tags = str_replace("/", " ", $tags);
$tags = str_replace("__", " ", $tags);
$tags = str_replace(".png", "", $tags);
$tags = str_replace(".jpg", "", $tags);
$tags = str_replace(".jpeg", "", $tags);
$tags = str_replace(".gif", "", $tags);
Everything works fine with the above but I also need it to replace some numbers at the start of the files I am adding
example of a file would be
247991 - my_small_house.jpg
its the numbers before the "-" I need gone
Can this be done?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将正则表达式与 preg_replace() 或 preg_split() 一起使用,但我认为explode() 会更好:
You can use a regex with preg_replace() or preg_split(), but I think explode() would be better:
您要删除的号码是由固定位数组成的吗?
如果是这样,你可以这样做:
否则,如果你确定每个数字都以“ - ”结尾,你可以这样做:
Is the number you have to delete made of a fixed number of digits?
If so, you could just do:
Else, if you're sure that every number ends with " - ", you could do:
试试这个:
Try this: