sed 's/\([^/]*\)\.phtml$/mydirectory\/\1.php/' <filename>
Will do that, if that’s what you need. (optionally with the -i flag to do an in-place replace.)
To break it up, first we have
s/<regexp>/<replacement>/
Which will replace what <regexp> matches with <replacement>. Next let’s look at the regexp we had:
\([^/]*\)\.phtml$
First, at the end we have \.phtml$ which will look for the string .phtml at the end of the line. $ anchors the regexp to the end of the line, and there’s a backslash in front of the dot to escape it, because ordinarily a dot matches anything.
After that what we have left is:
\([^/]*\)
Looking in the middle we have [^/] which will match one character ([] will match one of the characters inside the square brackets), which can be anything that isn’t a slash, because ^ will do a negated match, so ^/ inside square brackets “anything other than a slash”. After the closing square bracket there is an asterisks, *, which means that it will match one or more of the characters inside the square brackets.
Then wrapped around the above we have \( and \) which will capture what is matched inside the parenthesis and let you use them in the <replacement> section of s/<regexp>/<replacement>/ so we can use them.
And then in the <replacement> section we have:
mydirectory\/\1.php/
Which will replace the matched rexecp with first mydirectory/, the slash needing to be replaced since / is used as the separator for the sed replace, and then \1 is used to put in the text that was captured in the first capture group, and then we add the .php extension at the end.
All of this taken together means that we will capture everything from the last / to .phtml, add mydirectory/ after the last slash, write back the text we captured, and then add the .php extension.