Two solutions:
- preg_match()
- str_replace() with pathinfo()
1: preg_match()
Ok, so the problem is that you are using backslashes. You have to make sure that you do not use double quotes for defining your filepath, since the backslash is interpreted as an escape sequence. Use single quotes.
Additionally, with a regex, it's much simple to get a filename by moving from the back of the path until you hit a backslash.... the trick is that a backslash is \\\\
.. here's why
Finally, you don't want to use preg_replace. Just find the filename with preg_match:
2: str_replace() with pathinfo()
As others said, basename()
is a good option. Another option, if there's any chance that you may also need the directory or other path information later down the line is to use pathinfo()
The problem is that both basename and pathinfo assume forward slashes, so you must convert your backslashes to forward slashes:
Example: