B
B
Big_person2016-04-04 14:32:08
PHP
Big_person, 2016-04-04 14:32:08

How to add a subdirectory before the last slash to the url?

We have a URL like
site.ru/subdir1/subdir2/subdirN/filename.jpg
How can I add another directory with a specific name before /filename.jpg using php and regular expressions.
The output should be the line
site.ru/subdir1/subdir2/subdirN /newsubdir /filename.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2016-04-04
@Big_person

dirname($path).'/'.$dir.'/'.basename($path)

S
Saboteur, 2016-04-04
@saboteur_kiev

Try something like this.
I don’t have php at hand, I can’t check whether it is necessary to escape the normal slash in the second argument.

url=preg_replace("/(\w+)\/(w+.jpg)/i", "\$1/newsubdir/\$2", $url);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question