L
L
Lavrov952018-03-17 18:00:36
PHP
Lavrov95, 2018-03-17 18:00:36

How can str_replace only last?

Like str_replace only the last . on _home

$string = 'thumbs/2015/03/220px-Harvard_Wreath_Logo_1.svg-pvWrLY0xXuP1RbkQ8fJkHgbHFqKnmciQ.png'

should be
thumbs/2015/03/220px-Harvard_Wreath_Logo_1.svg-pvWrLY0xXuP1RbkQ8fJkHgbHFqKnmciQ_home.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-03-17
@demon416nds

possible extensions are known in advance?
(\.)(png|jpg|итд)$

R
riot26, 2018-03-17
@riot26

$string = 'thumbs/2015/03/220px-Harvard_Wreath_Logo_1.svg-pvWrLY0xXuP1RbkQ8fJkHgbHFqKnmciQ.png';
$string = preg_replace('/(\.[^\.]+$)/', '_home$1', $string);
echo $string;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question