If I have the following PATH as a string:
dirname/user folder/sample_file.png
How would I add the string _trash after the dirname/user folder in the PATH, so its result is:
dirname/user folder_trash/sample_file.png
This should work for you:
<?php
$path = "dirname/user folder/sample_file.png";
echo $path = dirname($path) . "_trash/" . basename($path);
?>
Output:
dirname/user folder_trash/sample_file.png
EDIT:
You can search for the folder and then just replace it with the new one:
<?php
$path = "dirname/user folder/another folder/sample_file.png";
$searchFolder = "/user folder/";
$replaceFolder = "user folder_trash";
echo $path = preg_replace($searchFolder, $replaceFolder, $path, 1);
?>
Output:
dirname/user folder_trash/another folder/sample_file.png
EDIT 2:
IF the sting follows the same pattern every time this should work to change the second folder:
<?php
$path = "dirname/user folder/another folder/another folder/sample_file.png";
$parts = explode("/", $path);
$parts[1] = $parts[1] . "_trash";
echo $path = implode("/", $parts);
?>
Output:
dirname/user folder_trash/another folder/another folder/sample_file.png
_trash?user folder is sample folder name only because there's a specific folder name for each user so i couldn't use the $searchFolder = "/user folder/";Another way:
You also use explode function to split string to items (using
count function to get number of item).
Then modify each item. It up to you.
Finally, merge items to string by string concatenation