I want to give the possibility to match string with wildcard *.
*
Example
$mystring = \'dir/folder1/file\'; $pattern = \'dir/*/file\'; string
You should just use .* instead.
.*
$pattern = str_replace( '*' , '.*', $pattern); //> This is the important replace
Edit: Also your ^ and $ were in the wrong order.
^
$
Working demo: http://www.ideone.com/mGqp2